How to Convert int to str in Python
How to Convert int to str in Python
In the last article, we studied how we convert a variable with string datatype to integer datatype and discussed different data types available in Python. In this section, we will see the ways & methods that can convert an integer data type to a string data type.
Four ways can help us in the conversion-
- Using str()
- Using f-string
- Using the "%s" keyword
- Using. format () function
First, let us discuss how we can convert int to str in python using str()
int= 15 #declaration of variable print(type(int)) str_var=str(int) #converting the data type print(type(str_var)) print(int+15) print(str_var+’15’)
- In the first line, we have declared the number 15 as an integer.
- To verify its data type, we will apply the type function, it will display the output as- <class ‘int’>.
- The next step is to convert our data type from integer to string using the explicit type conversion and assign the string value to a new variable called ‘str_var’.
- On checking the data type, we will find that its datatype is an integer. (it will display the output as <class 'str’>.
- It’s time to check on what results in the variables int & str_var give when we perform operations on them.
- So, when we simply add the variable int with 15 print(int+15), it will display the output as 30.
- If we directly add 15 to the variable str_var, it will display an error because of the difference in data types, so we will write 15 as a string here
print(str_var+'15')
- On running the given program, this print statement would give output as 1515.
Next, let’s have a look at how the same functionality can be performed by using f-string.
F-string in Python is a formatting mechanism that is also known as Literal String Interpolation.
The values declared as a string in Python can be embedded directly in the statement which we want to display using the print statement.
Let’s look at an example that illustrates the working of f-string.
a='Python' b='Tutorial and Example' print(f"You will find different concepts of {a} at {b}")
The f-string technique would replace the variables a and b with their respective values and would display them in the output of the print statement.
int= 15 #declaration of variable print(type(int)) str_var=f'{int}' #converting the data type print(type(str_var)) print(int+20) print(str_var+'20')
On checking the data type, we'll find that the f-string technique converts the datatype of our variable and the output of print(type(str_var)) comes out to be <class ‘str’>.
The outputs that will be obtained after the execution of the last two print statements would be 35 and 1520.
Now, we will see how we can convert an integer to a string using "%s" keyword.
%s keyword performs the following functions-
- Performs concatenation of two strings.
- It is used to convert any value to a string data type.
- It formats a value inside a string.
int=15 #declaration of variable print(type(int)) str_var="% s" % int #converting the data type print(type(str_var)) print(int+10) print(str_var+'25')
On executing the given program, we'll observe that the %s keyword converts the integer data type to a string datatype. This can be verified by using print(type(str_var)).
Last is using. format() function,
Placeholders are used in a print statement when we are using the format function. In the parentheses, we provide the variables that replace these placeholders with their respective values.
Let’s try to understand this with the help of a simple example-
a=10 b=20 add_num=a+b print("Sum of {} and {} is {}".format(a,b,add_num))
In the given program, we have declared the variables a and b as 10 and 20 respectively. After that to find the sum, we will store the value of a+b in a variable called add_num. Then in the print function, we will provide placeholders which will be replaced by the values of variables a,b, and add_num using the format function.
It can be used in our program in the following way-
int=15 #declaration of variable print(type(int)) str_var="{}".format(int) #converting the data type print(type(str_var)) print(int+10) print(str_var+'25')
The data types of both variables can be verified using the type function. On executing the given program, we will observe that the data type of int is an integer and the data type of str_var is a string.
So, in this article we discussed the methods of converting an integer data type to a string data type using str(), %s keyword, .format(), and f-string.