The world of programming is all about using strings and numbers. You’ll use these data types extensively, and work with them in everyday use. Sometimes you’ll need to convert between them, particularly when a language like Python is fussy about the difference.
If you are dabbling in Python, it’s useful to learn the nuances of moving from one data type to another, seamlessly.
In this article, we’ll demonstrate a few useful ways of converting a Python int to a string.
Convert a Python Number to String
Often, you might want to print a simple message which is a mix of strings and numbers. But given Python’s restrictions, you might need to adjust your approach to use the print statement correctly.
For example, Python doesn’t make it easy to print messages composed of separate parts, like:
I am Gaurav, and my age is 28 years.
Where Guarav is stored in one variable, and 28 in another.
However, Python does make integer to string conversion rather simple. To convert an int to a string, Python offers you various functions, each of which is easy to use.
Here’s a piece of code that prompts for user input:
Name = input ('Enter your name: ')
Age = 28
print ('I am ' + Name + '. I am ' + Age + ' years old.')
The desired output is:
I am Gaurav. I am 28 years old.
Instead, Python throws the following error:
File "<string> TypeError: can only concatenate str (not "int") to str"
This means you can’t combine a numeric and a string value using the + operator.
1. Use of str Function
The str function converts an integer into a string value. You have to use it as a prefix for any integer values.
The same example can be rewritten as:
Name = input ('Enter your name: ')
Age = str(28)
print ('I am ' + Name + '. I am ' + Age + ' years old.')
The desired output is:
The string value in the print statement is split into multiple parts so that the user-defined values are also incorporated.
2. Use of format() Function
Python offers another useful function to convert a number into a string. The format() functionis a faster and cleaner method for converting integers into strings.
The format function converts the integer into a string value before performing any computations.
# The integer value
Number = 100
# Convert the integer into a string using the format function
Str_value = '{}'.format(Number)
# Print the result
print('Your number is ', Str_value)
3. Use of Positional Formatting
Positional formatting is one of the oldest methods in Python to convert an integer into a string.
To use positional formatting, use the “%s” % prefix in front of the number, and Python will do the rest.
For example:
# The integer value
Number = 100
# Convert the integer into a string using positional formatting
Str_value = "%s" % Number
# Print the result
print ('Your number is ', Str_value)
The resulting output is:
4. Use of __str__() Method
Like the str() function, there is a __str__() method in Python, which is a close alternative for converting integers to strings. The __str__() gives a direct string representation of the integer.
For example:
# The integer value
Number = 100
# Convert the integer into a string using the __str__ method
Str_value = Number.__str__()
# Print the result
print('Your number is ', Str_value)
The output is as follows:
Using Python Functions to Convert Integers Into Strings
Python is a user-friendly language, which is quite efficient for programming, data manipulation, and other data-related uses.
If you are planning to learn Python, there are a lot of renowned Python courses available online, which can be valuable resources to grasp the basics.