Introduction to Python Int to String
In Python, all the values we use or store as a variable will have a unique data type. It explains the nature of the value and depending on that python automatically allocates a data type for that value. In this article, we are going to see int to string conversion in python using four different methods which convert the given integer value to the string value.
Syntax:
Python has different data types for a different set of values, Integers deals with numbers and strings that could take alphanumeric values and python allows different data structures like list, Tuple, Dictionary & sets for working with different problems. In the below example we have declared 2 different values and checked the data types of the values to give an idea of the python data types.
x='Its a Beautiful World'
y=85
print(type(x))
print(type(y))
Output:
Inferring from the above example we could understand the string data type and integer datatypes clearly. We have given a statement inside quotes and assigned it to the variable ‘x’ it’s an example of a string data type and the variable ‘y’ is a simple numeric character.
How to Convert an Integer to String in Python?
In python, the integer data type can be converted to string data type by using four methods. They are as follows.
- Using str() function
- Using %s string
- Using .format() function
- Using f- string
1. str() Method
The syntax of str() function is given below.
Syntax:
str(integer)
The str() function converts the value passed in the str() function and returns the string data type. The object can be char, int, or even a string. If no value is passed in the function it returns no value.
Example
Let us see a simple code to illustrate int to string conversion.
#int to string conversion
x = 10
# print the date type of variable x
print(type(x))
# convert the variable x into string
y = str(x)
# check and print type of y variable
print(type(y))
Output:
In the above method, we have declared an integer value of 10 to the variable ‘x’ and we converted it using the str() function in python, and the resulting ‘y’ variables returns as a string data type.
Example
Many operations like slicing, replacing, concatenation, etc. can be done with the string data type. Let us discuss another example as shown below
#code to print string value
a = 'Its a beautiful day'
print(a[1])
print(a[2:7])
Output:
In the above code, we have declared the string ‘Its a beautiful day’ to the variable a. In the next line, we have printed the sliced 1st position of the string which is also a string. In the next line, we have sliced the 2nd and 7th positions in the string and printed it. The string data type has versatile features and is easy to work with.
2. Int to String using %s Keyword
Syntax:
"%s" % integer value
Example
# int to string conversion using %s
a = 5
# print the value and data type of variable a
print(a)
print(type(a))
# convert the integer a into string and print the value and data type
b = "% s" % a
print(b)
print(type(b))
Output:
In the above program, we have assigned an integer value of 5 to the variable and printed the value and data type of a. In the next part, we have converted the integer a to string b using the %s keyword. In the last step, we have printed the data type of b and checked the result. Let us see another example where we will add two integer values and convert them to a string.
Example
#code to add two int values and convert the result to string
a = 10
b = 5
c = a + b
print(type(c))
x = '%s' %c
print(type(x))
Output:
Just like the previous example we are adding two int values and assigning the result to c and printing the data type. In the next part, we are converting int to string and checking the data type.
3. Int to String Conversion Using f- String
Syntax:
f'{integer_value}'
Example
# int to string conversion using f- string
a = 5
print(type(a))
b = f'{a}'
print(type(b))
Output:
4. Int to String Conversion Using .format() Method
Syntax:
'{}'.format(integer_value)
Example
#int to string conversion using .format() method
name = "Bala"
age = 26
# using .format() method
print("{name} is {age} years old".format(name=name, age=age))
Output:
In the above code, we have assigned the string “Bala” to the variable called name and assigned the int value of 26 to the variable age. In the next step, we are converting the age variable to string using the .format() function and printing the string as shown in the output.
Example
In the below example, we have got two string values as input and made an addition operation and printed the data type of the resulting value.
#code to sum of two input strings and convert it to int value
x = input('Enter first integer value:\n')
x=int(x)
y = input('Enter second integer value:\n')
y = int(y)
sum_inputs=x+y
print('sum of Inputs:{}'.format(sum_inputs))
print(type(sum_inputs))
Output:
In the above code, we have declared two inputs x and y as a string and performed a summation operation, and converted them to integer values and the resulting sum is printed as output. If we do not convert the values into int and perform an addition operation we would get the resulting output.
In this output above we have declared two string values and performed a sum operation since they are string values, the output is just a combination of both the strings mathematical operation is not performed since its a string value.
Conclusion
In this article, we have discussed int to string conversion using various examples. The data type conversion is considered to be very important in python since it is used in various real-time scenarios. When working with different kinds of algorithms in the data science field we need to convert data types.
Recommended Articles
This is a guide to Python Int to String. Here we discuss the definition and how to convert an integer to a string in python along with different examples and code implementation. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses