Python

Python Object to String

Converting Objects into Strings is quite crucial, especially when it comes to transferring data from one place to another, as strings are much easier to transfer than objects. However, in Python, objects are everywhere, and this basically means that everything in Python is, at its core, an object, even the predefined data types. Therefore, in this guide, str() and repr() methods will be used to convert various types of objects into strings and confirm by using the type() method.

Method 1: Using the str() Method

The str() method is actually the constructor method for building instances of the string class, and therefore it can be used to convert objects into Python strings. Let’s go over some examples of using the str() method to convert built-in data type objects and user-defined class objects into strings.

Example 1: Converting Built-in Data Type Objects Into Strings

To demonstrate this, take the following code to create variables:

intVar = 123
floatVar = 22.22
listVar = [121,33,'Google',12]

 

As you can see, you have created three variables, each of a different data type object, to confirm this use the type() method:

print(type(intVar))
print(type(floatVar))
print(type(listVar))

 

When this code is executed, it produces the following result on your terminal:

Let’s convert them all using the str() method, and confirm their type using the type() method:

strIntVar = str(intVar)
strFloatVar = str(floatVar)
strListVar = str(listVar)

print("\nAfter Conversion")
print(type(strIntVar))
print(type(strFloatVar))
print(type(strListVar))

 

When this code is executed now, it will produce the following outcome on the terminal:

This output confirms that you have successfully converted various Python Objects into Python Strings.

Example 2: Converting Custom Class Objects Into Strings

To demonstrate this, create a basic custom class using the following code snippet:

class LHClass:
    def __init__(self):
        print("New Object Created")

 

Create an object of this class, and print its type using the type() method:

objLHClass = LHClass()
print(type(objLHClass))

 

When this code is executed now, it will produce the following result on the terminal:

As you can see, the type of this object is the custom class “LHClass”. Create a new variable by using the str() constructor and pass the object of LHClass as an argument and print out its type with the type() method:

convObj = str(objLHClass)
print("\nAfter Conversion")
print(type(convObj))

 

When this code is executed, it will produce the following results on the terminal:

The output confirms that the object of a custom class can also be converted into a Python string using the str() method.

Method 2: Using the repr() Method

The repr() method works almost identically to the str() method, and to demonstrate the use of this method, create an object of the integer data type:

varInt = 420
print(type(varInt))

 

To convert it into a string, pass the variable inside the repr() method, store the result in a separate variable and display its type using the type() method:

convVar= repr(varInt)
print("\nAfter Conversion")
print(type(convVar))

 

When this code is executed, it will show the following results on your terminal:

You have successfully converted an integer object in a Python String, and you can even confirm that there is no change in the value by printing both the variables on to the terminal:

print(varInt)
print(convVar)

 

Append the above two lines in the code snippet and then execute the code to get the following verification on the terminal:

As you can see that there is no change in the value of the variables at all.

Conclusion

If you are looking for a way to convert Python Objects into Python Strings, then you can utilize the str() constructor method and the repr() method. To use both of these methods, simply pass the object to be converted in the arguments and store the return value. To confirm the conversion, you can use the type() method along with the print() statement. These methods can be applied on basic data type objects as well as objects of a user-defined class.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.