Convert Object to String in Python Last Updated : 22 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Python provides built-in type conversion functions to easily transform one data type into another. This article explores the process of converting objects into strings which is a basic aspect of Python programming.Since every element in Python is an object, we can use the built-in str() and repr() methods to convert any built-in object into a string representation. Let's see how these methods work with the help of an example.str() methodstr() is a built-in method so don't need to import any library, we can simple use it over the targeted object. Python i = 6 # int object a = [1, 2, 3, "Hello"] # list object # Converting to string s1 = str(i) print(s1) print(type(s1)) s2= str(a) print(s2) print(type(s2)) Output6 <class 'str'> [1, 2, 3, 'Hello'] <class 'str'> repr() methodrepr() method is a bit different from str(), it gives a more detailed string representation of an object. Unlike str(), which focuses on making output user-friendly, repr() provides a more technical view which is helpful for debugging and often includes object type or structure and its also customizable. Let's looks at some examples,Example 1: Normal list conversion Python # Using repr() with a list a = [1, 2, 3, "Hello"] print(repr(a)) print(type(repr(a))) Output[1, 2, 3, 'Hello'] <class 'str'> Example 2: Converting objects of user defined classes with and without "__repr__()" method. Python # Class without __repr__() class Animal: def __init__(self, species, sound): self.species = species self.sound = sound # Class with __repr__() class Person: def __init__(self, name, age): self.name = name self.age = age def __repr__(self): return f"Person(name='{self.name}', age={self.age})" # Creating objects a = Animal("Dog", "Bark") p = Person("Prajjwal", 22) # Using repr() on both objects print("Without __repr__():", repr(a)) print("With __repr__():", repr(p)) OutputWithout __repr__(): <__main__.Animal object at 0x7f2b98ac2900> With __repr__(): Person(name='Prajjwal', age=22) Explanation: Without __repr__(): Python uses a default format, which just shows the object's type and memory address (something like <__main__.Animal object at 0x7f9d8c3f2d30>).With __repr__(): The object gets a more meaningful and readable description, based on what we define in the __repr__() method.Note: To know a lot more interesting things about str() and repr() and the difference between them, refer to str() vs repr() in PythonRelated Articles:Python str() functionPython repr() FunctionPython Built in FunctionsPython object Comment More infoAdvertise with us Next Article Convert Object to String in Python D deepanshumehra1410 Follow Improve Article Tags : Python python-string Practice Tags : python Similar Reads Convert String to Int in Python In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver 3 min read Convert String to Long in Python Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing information about converting a string to long. Converting String to long A long is an integer type value that has unlimited length. By converting a string into long we are transl 1 min read Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is 2 min read Convert integer to string in Python In this article, weâll explore different methods for converting an integer to a string in Python. The most straightforward approach is using the str() function.Using str() Functionstr() function is the simplest and most commonly used method to convert an integer to a string.Pythonn = 42 s = str(n) p 2 min read Convert Hex to String in Python Hexadecimal (base-16) is a compact way of representing binary data using digits 0-9 and letters A-F. It's commonly used in encoding, networking, cryptography and low-level programming. In Python, converting hex to string is straightforward and useful for processing encoded data.Using List Comprehens 2 min read Convert Set to String in Python Converting a set to a string in Python means changing a group of unique items into a text format that can be easily read and used. Since sets do not have a fixed order, the output may look different each time. For example, a set {1, 2, 3} can be turned into the string "{1, 2, 3}" or into "{3, 1, 2}" 2 min read Convert String to Set in Python There are multiple ways of converting a String to a Set in python, here are some of the methods.Using set()The easiest way of converting a string to a set is by using the set() function.Example 1 : Pythons = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s) print(type(set_s)) pr 1 min read Byte Objects vs String in Python In Python 2, both str and bytes are the same typeByte objects whereas in Python 3 Byte objects, defined in Python 3 are "sequence of bytes" and similar to "unicode" objects from Python 2. In this article, we will see the difference between byte objects and strings in Python and also will look at how 3 min read Python - Convert Dictionary Object into String In Python, there are situations where we need to convert a dictionary into a string format. For example, given the dictionary {'a' : 1, 'b' : 2} the objective is to convert it into a string like "{'a' : 1, 'b' : 2}". Let's discuss different methods to achieve this:Using strThe simplest way to conver 2 min read Convert Decimal to String in Python Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting decimal to string. Converting Decimal to String str() method can be used to convert decimal to string in Python. Syntax: str(object, encoding=âut 1 min read Like