In this article we will discuss 5 different ways to convert a integer to string in python.
Using str() to convert an int to string in python
Python provides a function str(object). It accepts an object as an argument and returns a string representation of that object. So, if we pass the an integer as an argument to the str() function, then it will return a string representation of that integer.
So, let’s use this to convert int to string i.e.
# int value num = 51 # Convert an int to string value = str(num) print('Value: ', value) print('Type : ', type(value))
Output:
Value: 51 Type : <class 'str'>
Type of variable value is <class ‘str’>, which confirms that integer was successfully converted to a string value.
Using __str__() to convert an integer to string in python
str(object) function internally calls the __str__() on the passed argument object. So, we can directly call the __str__() on the object. In our case we passed integer to str() function in previous example. Instead of it, we can directly call the __str__() function on int object to get a string representation of the integer i.e.
Frequently Asked:
- Python String count() function
- How to Get a Function Name as a String in Python?
- Convert a List to a String in Python
- Remove special characters from a string in python
# int value num = 51 # Convert an int to string value = num.__str__() print('Value: ', value) print('Type : ', type(value))
Output:
Value: 51 Type : <class 'str'>
Type of variable value is <class ‘str’>, which confirms that integer was successfully converted to a string value.
Using f-strings to convert an int to string in python
f-strings were introduced in python 3.6. It is represented by string literals that have an f at the beginning and curly braces containing expressions after that. Variables in the expression will be replaced by values during evaluation at runtime.
So, let’s use this to convert int to string i.e.
# int value num = 51 # Convert an int to string value = f'{num}' print('Value: ', value) print('Type : ', type(value))
Output:
Value: 51 Type : <class 'str'>
It is the most simple and fastest solution.
Type of variable value is <class ‘str’>, which confirms that integer was successfully converted to a string value.
Using format() to convert an integer to string in python
str class in python provides a format() function. It converts the string by replacing the placeholders defined inside {} to values of the variables. So, let’s use this to convert int to string i.e.
# int value num = 51 # Convert an int to string value = "{}".format(num) print('Value: ', value) print('Type : ', type(value))
Output:
Value: 51 Type : <class 'str'>
Type of variable value is <class ‘str’>, which confirms that integer was successfully converted to a string value.
Using positional formatting to convert an int to string in python
This is the oldest approach. Using positional formatting, we can insert objects inside a string.
So, let’s use this to convert int to string i.e.
# int value num = 51 # Convert an int to string value = "%s" % num print('Value: ', value) print('Type : ', type(value))
Output:
Value: 51 Type : <class 'str'>
Type of variable value is <class ‘str’>, which confirms that integer was successfully converted to a string value.
So, these were the 5 different ways to convert an integer to string in python.
The complete example is as follows,
def main(): print('*** Convert an int to string in python ***') print('*** Using str() to convert an integer to string in python ***') # int value num = 51 # Convert an int to string value = str(num) print('Value: ', value) print('Type : ', type(value)) print('*** Using __str__() to convert an integer to string in python ***') # int value num = 51 # Convert an int to string value = num.__str__() print('Value: ', value) print('Type : ', type(value)) print('*** Using f-string to convert an integer to string in python ***') # int value num = 51 # Convert an int to string value = f'{num}' print('Value: ', value) print('Type : ', type(value)) print('*** Using format() to convert an integer to string in python ***') # int value num = 51 # Convert an int to string value = "{}".format(num) print('Value: ', value) print('Type : ', type(value)) print('*** Using positional formatting to convert an integer to string in python ***') # int value num = 51 # Convert an int to string value = "%s" % num print('Value: ', value) print('Type : ', type(value)) if __name__ == '__main__': main()
Output:
*** Convert an int to string in python *** *** Using str() to convert an integer to string in python *** Value: 51 Type : <class 'str'> *** Using __str__() to convert an integer to string in python *** Value: 51 Type : <class 'str'> *** Using f-string to convert an integer to string in python *** Value: 51 Type : <class 'str'> *** Using format() to convert an integer to string in python *** Value: 51 Type : <class 'str'> *** Using positional formatting to convert an integer to string in python *** Value: 51 Type : <class 'str'>