In Python, you can use the built-in str()
Function to convert an integer to a string. Here are some common methods:
use str()
Function:
num = one hundred and twenty-three str_num = str (num) print (str_num)
The output result is: one hundred and twenty-three
In this example, we use str()
Function will integer num
Converts to a string.
Use format string:
num = four hundred and fifty-six str_num = "%d" % num print (str_num)
The output result is: four hundred and fifty-six
In this example, we use the format string method to format the integer num
Convert to string, %d
Represents an integer type.
Use f-string (format string literal):
num = seven hundred and eighty-nine str_num = f" {num} "
print (str_num)
The output result is: seven hundred and eighty-nine
In this example, we use f-string to convert the integer num
Convert to a string and put the variable directly in curly braces.
Whether using str()
Functions, formatted strings, or f-strings can all convert integers to strings.