# Python: How to Convert Integer to String

Integer:

An integer, on the other hand, is a whole number, the kind you started counting out in school when you first heard about numbers. In Python 3, an integer can be any length up to the computer’s memory limit, so you’ll probably never run out of integers to use.

String:

In computer programming, a string is a sequence of characters that can be used as a literal constant or as a variable. A string is a finite sequence of symbols chosen from an alphabet in formal languages, which are used in mathematical logic and theoretical computer science.

Example:

Input:

IntegerValue=23

Output:

StringValue: 23
Type : <class 'str'>

## Integer to String conversion

The following is a list of possible python methods for converting an integer to a string:

### Method #1: Using str() function

The str() function in Python can be used to convert an integer to a string. Any python data type can be passed to the str() function, which converts it to a string.

The str() function converts an object to a string. An int, char, or string may be used as the object. It returns an empty string if the object is not passed as an argument.

Below is the implementation:

# given integer
intvalue = 23
# converting to string using str() function
strvalue = str(intvalue)
# printing the value and type
print("String value :", strvalue)
print("type :", type(strvalue))


Output:

String value : 23
type : <class 'str'>

### Method #2: Using “%s” keyword

This is the most traditional method. We may insert objects within a string using positional formatting.

Below is the implementation:

# given integer
intvalue = 23
# converting to string using str() function
strvalue = "%s" % intvalue
# printing the value and type
print("String value :", strvalue)
print("type :", type(strvalue))


Output:

String value : 23
type : <class 'str'>

### Method #3:Using format() function

Python’s string class has a format() feature. It converts the string by substituting the values of the variables for the placeholders specified within.

Below is the implementation:

# given integer
intvalue = 23
# converting to string using format() function
strvalue = "{}".format(intvalue)
# printing the value and type
print("String value :", strvalue)
print("type :", type(strvalue))


Output:

String value : 23
type : <class 'str'>

### Method #4: Using f-strings

F-strings allow you to use a simple syntax to insert expressions within string literals. It’s worth noting that an f-string is actually an expression that is evaluated at runtime rather than a fixed value. An f-string is a literal string in Python source code that includes expressions within braces and is prefixed with ‘f’. The values are substituted for the expressions.

Below is the implementation:

# given integer
intvalue = 23
# converting to string using f-strings
strvalue = f'{intvalue}'
# printing the value and type
print("String value :", strvalue)
print("type :", type(strvalue))


Output:

String value : 23
type : <class 'str'>

### Method #5: Using __str__()

Internally, the str(object) function calls __str__() on the passed argument object. As a result, we may use the object’s __str__() method directly. In the previous example, we passed an integer to the str() function. Instead, we can use the int object’s __str__() function to get a string representation of the integer.

# given integer
intvalue = 23
# converting to string using __str__
strvalue = intvalue.__str__()
# printing the value and type
print("String value :", strvalue)
print("type :", type(strvalue))


Output:

String value : 23
type : <class 'str'>

Related Programs: