Examples
- Palindrome Program in Python (Check String is Palindrome or Not)
- Program to Remove Punctuations From String in Python
- Remove a Character from String in Python (Program With Examples)
- Remove Stop Words from String in Python Using NLTK and spaCy
- Program to Sort Words in Alphabetical Order in Python (Arrange in Alphabetic Order)
- How to Sort Strings in Python? (Program With Examples)
- How to Count Vowels in Python String? Vowels Program in Python
- How to Remove Vowels from String in Python? Program With Examples
- How to Convert String to Int or Float in Python? String Parse Program
- How to Convert Float to Int in Python? Program With Examples
- How to Convert Int to String in Python? Program with Examples
- Remove Spaces from String in Python (Trim Whitespace)
- Python Program to Check If Two Strings are Anagram
- How to Capitalize First Letter in Python? String Capitalization Program
- Find All Permutations of String in Python (Programs and Examples)
- Find All Substrings of a String in Python (Programs with Examples)
- Create Multiline String in Python (With & Without New Line)
How to Convert Int to String in Python? Program with Examples
In this tutorial, we will learn how to convert int to string in Python programming. Whether you're a beginner or an experienced Python developer, understanding the process of converting integer to string is a basic skill that can greatly enhance your ability to handle and manipulate data effectively.
It is a common requirement in various programming scenarios. By transforming numeric values into string representations, you gain the flexibility to format and present data in a more human-readable manner.
Below are the main scenarios where the int to str conversion is useful:
-
Data Presentation and Reporting: It is essential for generating reports, displaying data in user interfaces, or formatting output in a specific way. By converting numbers to strings, you can incorporate custom formatting, add units of measurement, or concatenate them with other text to create informative and well-structured outputs.
-
File Operations and Data Serialization: When dealing with files, especially in scenarios involving text files or data serialization, converting integers to strings ensures proper formatting and consistency. This is particularly useful when storing numerical data as strings in file formats or when working with data serialization libraries that require string representations.
-
Concatenation and Dynamic String Construction: In many programming tasks, you may need to build dynamic strings that incorporate both textual and numerical data. Hence, if you know how to convert int to float in Python, you can easily concatenate them with other strings, construct complex messages, or generate unique identifiers based on numeric values.
To practice and master the concepts discussed in this tutorial, we recommend using the online Python compiler provided by Tutorials Freak. This platform offers a user-friendly and interactive coding environment that allows you to write, execute, and experiment with Python Programs directly in your web browser.
Int to string in Python Using ‘%s’
To convert an integer to a string in Python using the '%' formatting operator with 's', you can use the `%` operator followed by the letter 's' inside a string.
Code
num = 20
# Check data type of "num" variable
print('Before conversion:', type(num))
# Convert integer to a string
converted_num = "% s" % num
# Data type after int to string conversion
print('After conversion:', type(converted_num))
Output
Before conversion: class 'int'
After conversion: class 'str'
Explanation
Let's break down the code step by step:
-
num = 20: This line assigns the value 20 to the variable num.
-
print('Before conversion:', type(num)): Here, we use the print() function to display the message 'Before conversion:' along with the data type of the num variable. The type() function is used to determine the data type of a variable.
-
converted_num = "% s" % num: In this line, the value of the num variable is converted to a string using the % operator and the % s format specifier. The resulting string is assigned to the variable converted_num.
-
print('After conversion:', type(converted_num)): Similarly, this line prints the message 'After conversion:' along with the data type of the converted_num variable.
Python Program to Convert Integer to String Using .format() Function
Here's an alternative program that uses the .format() function for converting an integer to a string:
Code
num = 20
# Check data type of "num" variable
print('Before conversion:', type(num))
# Convert integer to a string using .format() function
converted_num = "{}".format(num)
# Data type after int to string conversion
print('After conversion:', type(converted_num))
Output
Before conversion: class 'int'
After conversion: class 'str'
Explanation
The program is similar to the previous version, but instead of the % operator, we use the .format() function to convert the integer num to a string. The {} acts as a placeholder in the format string, and the .format() function replaces it with the value of num.
Converting Int to string in Python Using f-string
Here's the Python program to convert an integer to a string using an f-string:
Code
num = 20
# Check data type of "num" variable
print('Before conversion:', type(num))
# Convert integer to a string using f-string
converted_num = f"{num}"
# Data type after int to string conversion
print('After conversion:', type(converted_num))
Output
Before conversion: class 'int'
After conversion: class 'str'
Explanation
In this version, we use an f-string by prefixing the string with f. Inside the f-string, we enclose the variable num in curly braces {} to insert its value into the string.