How to Convert integer to string in Python

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:

  1. Using the str() function
  2. Using %s keyword
  3. Using f-string

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'>

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts

Leave a Comment