The simplest way to convert integer to string in Python is using the inbuilt str() function. However, there are several other ways also to convert an integer value to a string. These methods are listed below:
Let’s discuss each method one by one.
1. Using the str() function
The str() function takes an integer value as an input parameter and returns its equivalent string. Refer to the example below:
Example:
# Convert integer to string in Python using str() function num = 10 print('type(num): ',type(num)) num_str = str(num) print('type(num_str): ', type(num_str))
Output:
type(num): <class 'int'>
type(num_str): <class 'str'>
2. Using %s keyword
We can use %s as a prefix to convert an integer value to its equivalent string.
Syntax: ‘%s’ %integer_value
Example:
# Convert integer to string using %s keyword num = 10 print('type(num): ',type(num)) num_str = '%s' %num print('type(num_str): ', type(num_str))
Output:
type(num): <class 'int'>
type(num_str): <class 'str'>
3. Using f-string
Syntax: f'{int_value}’
Example:
# Convert integer to string in Python using f-string method num = 10 print('type(num): ',type(num)) num_str = f'{num}' print('type(num_str): ', type(num_str))
Output:
type(num): <class 'int'>
type(num_str): <class 'str'>