In this article, we’ll be learning how to cast an integer to a string in python. This is very useful if you are building a program that needs to convert data into the correct type of data. However, This is useful in Python to cast (means to change) the type of an integer variable to a string variable.The integer value needs to be converted into a string before performing computations. On this page, the demonstration with code examples is about how you can convert an integer to a string in python.
If you want to learn more about Python Programming, visit Python Programming Tutorials.
str() function approach to convert the integer to a string in Python
In Python, It’s possible to use the str() function to convert integer numbers into string variables in a Pythonic way. However, String variables of objects are returned by the str() function. Any number, character, or string can be used as an object.
__str__() returns the string object that performs as similar as the str() function does in the program.
a=28
print('a: ', str(a), " ", type(str(a)))
print('a: ', a.__str__())
a: 28 <class 'str'>
a: 28
<a href="a: 28
Injecting a number in a string
If there is a character of sequence and you want to add the number/integer type within the characters of the string, then it could be performed by the str() operation.
Note that the stream of characters does not place integer type directly within the statement. So it first needs to convert the number you want to invoke within the string using the str() function. Otherwise, it returns the TypeError, “TypeError: can only concatenate str (not “int”) to str”.
a = 29
print("The number is " + a)
TypeError: can only concatenate str (not "int") to str
string= str(a)
print("The number is " + string)
The number is 29
Using f-string
The F-string represents formatted string literals. String literals will be replaced with the values contained within the curly braces. The syntax format goes like this the f at the beginning, and within curly brackets, the variable will be passed that converts an integer to a string without using the str() function.
Example Code to cast an int to a string in Python.
year = 2027
str_year = f'{year}'
str_year
'2027'
Using Placeholder method
Using %d placeholder
In Pythonic code, we can print an integer or numeric value into a string using the %d placeholder. The placeholder syntax expression is that within the print command write the string you want to print and into the string call the placeholder(%d) and close the string within commas(‘ ’), after that place % operator for string formatting and then within the square brackets () pass an argument that contains the number.
The code execution is as following:
model = 2027
print("The car model is %d" % (model))
The car model is 2027
%s placeholder
%s placeholder for the strings. The expression of the placeholder is placed within commas, after that the % operator comes and then the numeric or integer values come that you want to be cast into the string in Python.
year=2027
"%s" % year
'2027'
format {} approach to casting an int to a string
In format(), a specific value is placed into the placeholder, which is indicated by the curly bracket.
During execution, braces {} are replaced by the value we want to print.
'{}'.format(2027)
'2027'
Conclusion
Overall, Python makes it quite easy to cast integer data types into strings. In Python, there are various situations where it is necessary to cast an int to a string. As we have seen, casting can be done in a variety of ways. Using it, you can convert any numeric type to a string type.