Python

Python Object to String

You may have used many types of conversion techniques in programming to convert one type of variable to another type. Like many other languages, python comes up with a simple function to convert one type variable to another type variable. Within this article today, we will discuss some very simple and easy-to-do codes to convert an object type variable to a string while working within the Python environment. It is as simple as it has been defined. So, let’s start with opening a console application in Ubuntu 20.04 system with the Ctrl+Alt+T keystroke.

Example 1:

You must have some python files to add code to it. So, we have executed the touch instruction with the name of a file to be created as per the output below. The file needs to be opened to add some Python script. Thus, we have been using the GNU Nano editor to quickly open the file within the shell using the nano keyword with the file name.

$ touch object.py

$ nano object.py

Right after the file is opened, we have added the python-support path as shown in the image code. We have initialized a variable named “obj” with integer values 13. The first print statement is displaying the value of this variable object and the next print statement is using the “type()” function to show the type of this variable “obj”.

Now, we have applied the “str()” function on the variable “obj” to convert it to the string type and save it to the variable “new”. The last print statement is displaying the type of a new converted variable “new”.

#!/usr/bin/python3

obj = 13

print(“The object is: ”, obj)

print(“Before: ”, type(obj))

new = str(obj)

print(“After: ”, type(new))

Run this Python code file with python3 extension as shown below. The result shows the object value 13 in the first line of output. The other two lines show the type of variable “obj” before and after the typecasting i.e., int and string.

$ python3 object.py

Example 02:

Let’s have another example of typecasting. We have initialized a float variable and printed its value and type at the shell. Then, we converted it to the integer type first and printed its type on the console terminal. In the end, we converted the integer value to the string type and displayed its type on the shell. The code has been saved to be displayed on the shell.

#!/usr/bin/python3

obj = 3.89

print(“The object is: ”, obj)

print(“Before Update, Type: ”, type(obj))

new1 = int(obj)

print(“After first Update, Type: ”, type(new1))

new2 = str(obj)

print(“After second Update, Type: ”, type(new2))

The code has been interpreted and executed with the python3 package on the shell. In return, we have got the value of the float variable first at the shell with its type “float” displayed. After this, the updated type of a variable has been displayed one after another as shown below.

$ python3 object.py

Example 3:

Let’s have our last example to convert an object type to a string. After opening the file, we have initialized two variable structures. One of them is a dictionary type and the other is a tuple. The print statement has been using the “repr()” function on both of them separately to convert them to a simple string and display them as it is on the shell.

A “Test()” class has been used here with its built-in function “__repr__” to return a simple string value to the print statement. The print statement has been calling the “repr()” function within it using the “test()” object.

#!/usr/bin/python3

d =  {“x”: 2, “y”:4}

l = [2, 4, 6, 8, 10]

print(repr(d))

print(repr(l))

class Test():

  def __repr__(self):

    return “Test Class…”

print(repr(Test()))

Run this code with the python3 keyword. You can see that not only the repr() function value but the dictionary, and tuple type variables have been converted into a string and displayed.

$ python3 object.py

Conclusion

This guide contains the conversion of any object variable to a string value. For this purpose, we have used the “str” method and repr() function in our codes. This guide will be quite helpful to all Python users.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content