
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change any data type into a string in Python?
In Python, converting different data types into strings is a common requirement when we want to display or store information in text format.
The built-in str() function is used for this conversion. This function accepts any Python object as a parameter and returns a string representation of it. The basic syntax of the str() function is as follows:
str(object)
Where object is any valid Python object, such as int, float, bool, list, etc. In this article, we will explore different data type conversions into a string in Python.
Conversion of Integer to String
An integer can be converted into a string using the str() function. This conversion is useful when we need to concatenate numbers with strings. Here is the example which converts the Integer to a string -
num = 42 string_num = str(num) print(string_num) print(type(string_num))
Here is the output of the above program -
42 <class 'str'>
Conversion of Float to String
A floating-point number is converted into a string to handle formatting or to include it in a message or statement. Following is the example which shows how to change the Float datatype to string -
pi = 3.14159 string_pi = str(pi) print(string_pi) print(type(string_pi))
Following is the output of the above program -
3.14159 <class 'str'>
Conversion of Boolean to String
In Python, a Boolean value represents one of two possible states as True or False. These values can be converted into strings using the str() function. This conversion is useful when including boolean values in output messages, logs, or text-based data formats.
Example
Following is an example which shows how to change a boolean value into a string datatype using the str() function -
is_active = True string_active = str(is_active) print(string_active) print(type(string_active))
Below is the output of the above program -
True <class 'str'>
Conversion of List to String
In Python, a list is a collection of elements stored in a single variable. In some cases, it is necessary to convert a list into a string.
For example, when we are joining the list elements to create a sentence or to store list data in a textual format, then this can be done by using the join() method, which concatenates list elements into a single string.
Example
Following is the example which shows how to convert a list of strings into a single string using the join() method along with the str() function -
fruits = ["apple", "banana", "cherry"] string_fruits = ", ".join(fruits) print(string_fruits) print(type(string_fruits))
Output of the above program -
apple, banana, cherry <class 'str'>
Conversion of Tuple to String
A tuple in Python is an ordered, immutable collection of elements. If we want to display tuple data as a single text string or write it into a file, then we need to convert it to a string datatype. The most common way to convert a tuple to a string is by using the str() function or the join() method, if the tuple contains only strings.
Example
Here is an example in which we convert the Tuple datatype to the string datatype by using the str() function -
my_tuple = (1, 2, 3) string_tuple = str(my_tuple) print(string_tuple) print(type(string_tuple))
Following is the output of the above program -
(1, 2, 3) <class 'str'>
Conversion of Dictionary to String
In Python, a dictionary is a data structure used to store key-value pairs. To convert a dictionary into a string, we can use the built-in str() function or more advanced methods such as json.dumps() for formatting and data serialization.
Example
In this example, we use the str() function to convert the dictionary into a human-readable string representation by including the curly braces, keys, and values.
person = {"name": "Alice", "age": 30, "city": "New York"} string_person = str(person) print(string_person) print(type(string_person))
Below is the output of the above program -
{'name': 'Alice', 'age': 30, 'city': 'New York'} <class 'str'>