How to change any data type into a String in Python? Last Updated : 02 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, it's common to convert various data types into strings for display or logging purposes. In this article, we will discuss How to change any data type into a string. Using str() Functionstr() function is used to convert most Python data types into a human-readable string format. It is the most commonly used method when we want a string representation of an object for printing or displaying to users. Python a = 123 str_a = str(a) print(str_a) Output123 1. Converting Float to String Python b = 45.67 str_b = str(b) print(str_b) # Output: "45.67" Output45.67 2. Converting Boolean to String Python c = True str_c = str(c) print(str_c) OutputTrue 3. Converting List to String Python d = [1, 2, 3] str_d = str(d) print(str_d) Output[1, 2, 3] Other methods we can use to convert data types to strings are:Table of ContentUsing str() FunctionUsing repr() FunctionUsing __str__() MethodUsing __repr__() MethodUsing repr() Function repr() function is used to convert an object into a string that is more formal or "developer-friendly." It is meant to provide a detailed, unambiguous string representation of the object that could often be used to recreate the object. 1. Converting Integer to String Python a = 123 repr_a = repr(a) print(repr_a) # Output: "123" Output123 2. Converting Float to String Python b = 45.67 repr_b = repr(b) print(repr_b) Output45.67 3. Converting List to String Python d = [1, 2, 3] repr_d = repr(d) print(repr_d) Output[1, 2, 3] 4. Converting Dictionary to String Python e = {"name": "Alice", "age": 25} repr_e = repr(e) print(repr_e) Output{'name': 'Alice', 'age': 25} Using __str__() MethodThe __str__() method is used to define how an object of a custom class is converted into a string when str() is called on it. This method is part of Python’s object-oriented programming, and it allows us to control the string representation of our own classes. Python class MyClass: def __str__(self): return "I am a custom object!" obj = MyClass() str_obj = str(obj) print(str_obj) OutputI am a custom object! Using __repr__() MethodThe __repr__() method is used to define how an object of a custom class is converted into a string when repr() is called. Like __str__(), it is a special method, but it is aimed at providing a more detailed or formal representation of the object for debugging or logging purposes. Python class MyClass: def __repr__(self): return "MyClass()" obj = MyClass() repr_obj = repr(obj) print(repr_obj) OutputMyClass() Comment More infoAdvertise with us Next Article How to Convert Bool (True/False) to a String in Python? A ashishguru9803 Follow Improve Article Tags : Python Python Programs python-string python-basics Practice Tags : python Similar Reads How to Convert Bool (True/False) to a String in Python? In this article, we will explore various methods for converting Boolean values (True/False) to strings in Python. Understanding how to convert between data types is crucial in programming, and Python provides several commonly used techniques for this specific conversion. Properly handling Boolean-to 2 min read How to take string as input from a user in Python Accepting input is straightforward and very user-friendly in Python because of the built-in input() function. In this article, weâll walk through how to take string input from a user in Python with simple examples. The input() function allows us to prompt the user for input and read it as a string. 3 min read How to Initialize a String in Python In Python, initializing a string variable is straightforward and can be done in several ways. Strings in Python are immutable sequences of characters enclosed in either single quotes, double quotes or triple quotes. Letâs explore how to efficiently initialize string variables.Using Single or Double 2 min read Convert tuple to string in Python The goal is to convert the elements of a tuple into a single string, with each element joined by a specific separator, such as a space or no separator at all. For example, in the tuple ('Learn', 'Python', 'Programming'), we aim to convert it into the string "Learn Python Programming". Let's explore 2 min read How to format a string using a dictionary in Python In Python, we can use a dictionary to format strings dynamically by replacing placeholders with corresponding values from the dictionary. For example, consider the string "Hello, my name is {name} and I am {age} years old." and the dictionary {'name': 'Alice', 'age': 25}. The task is to format this 3 min read Python | Convert heterogeneous type String to List Sometimes, while working with data, we can have a problem in which we need to convert data in string into a list, and the string contains elements from different data types like boolean. This problem can occur in domains in which a lot of data types are used. Let's discuss certain ways in which this 6 min read Like