
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to convert int to string in Python?
Type conversion is at times needed when the user wants to convert one data type into another data type according to requirement.
Python has in-built function str() to convert an integer to a string. We will be discussing various other methods in addition to this to convert int into string in Python.
Using str()
This is the most commonly used method to convert int into string in Python.The str() takes integer variable as a parameter and converts it into a string.
Syntax
str(integer variable)
Example
num=2 print("Datatype before conversion",type(num)) num=str(num) print(num) print("Datatype after conversion",type(num))
Output
Datatype before conversion <class 'int'> 2 Datatype after conversion <class 'str'>
The type() function gives the datatype of the variable which is passed as parameter.
In the above code, before conversion, the datatype of num is int and after the conversion, the datatype of num is str(i.e. string in python).
Using f-string
Syntax
f ’{integer variable}’
Example
num=2 print("Datatype before conversion",type(num)) num=f'{num}' print(num) print("Datatype after conversion",type(num))
Output
Datatype before conversion <class 'int'> 2 Datatype after conversion <class 'str'>
Using “%s” keyword
Syntax
“%s” % integer variable
Example
num=2 print("Datatype before conversion",type(num)) num="%s" %num print(num) print("Datatype after conversion",type(num))
Output
Datatype before conversion <class 'int'> 2 Datatype after conversion <class 'str'>
Using .format() function
Syntax
‘{}’.format(integer variable)
Example
num=2 print("Datatype before conversion",type(num)) num='{}'.format(num) print(num) print("Datatype after conversion",type(num))
Output
Datatype before conversion <class 'int'> 2 Datatype after conversion <class 'str'>
These were some of the methods to convert int into string in Python. We may require to convert int into string in certain scenarios such as appending value retained in a int into some string variable. One common scenario is to reverse an integer. We may convert it into string and then reverse which is easier than implementing mathematical logic to reverse an integer.
- Related Articles
- How to convert hex string into int in Python?
- How to convert int to String in java?
- How to convert an int to string in C++?
- How to convert Int to Hex String in Kotlin?
- Convert int to String in Java
- Convert String to Java int
- How to convert a String to an int in Java
- How to convert a string to int in Lua programming?
- How to convert a String into int in Java?
- How to convert a string into int in C#?
- How to convert a Java String to an Int?
- MongoDB query to convert string to int?
- Java Program to convert a String to int
- Java Program to convert int to binary string
- Java Program to convert int to Octal String
