How to Convert Integer into String in Python

When you build a message, write a value to a file, or create a filename from a counter, Python often needs the number as text. If you try to join a string and an integer with +, Python raises an error instead of converting the number for you, so you need to convert the integer to a string first.
This article explains how to convert a Python integer to a string with str() and f-strings, and how to format the result with leading zeros, thousands separators, or a different base.
Python str() Function
In Python, we can convert integers and other data types to strings using the built-in str() function.
The str() function returns a string version of a given object. For integer conversion, use the following syntax:
str(object)object- Object to be converted to a string.
Converting a Python Integer into String
To convert the integer 23 to a string, pass the number to the str() function:
str(23)'23'The quotes around 23 indicate that the value is no longer an integer but a string. The type() function confirms it:
type(str(23))<class 'str'>'), double ("), or triple quotes (""").Negative numbers keep their sign:
str(-15)'-15'Concatenating Strings and Integers
The most common reason to convert an integer is to combine it with text. Here is what happens when we try to concatenate a string and an integer using the + operator:
number = 6
lang = "Python"
quote = "There are " + number + " relational operators in " + lang + "."
print(quote)Python raises a TypeError exception because it cannot concatenate strings and integers:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to strTo fix the error, pass the integer to the str() function:
number = 6
lang = "Python"
quote = "There are " + str(number) + " relational operators in " + lang + "."
print(quote)This time, the code runs successfully:
There are 6 relational operators in Python.You do not always need str() to build a string from numbers. On Python 3.6 and later, you can use f-strings
, which are literal strings prefixed with f containing expressions inside braces. Python converts the integer for you:
number = 6
lang = "Python"
quote = f"There are {number} relational operators in {lang}."
print(quote)There are 6 relational operators in Python.F-strings are the easiest option to read and the one you will see most often in modern Python code.
The built-in string class also provides a format() method that formats a given string using an arbitrary set of positional and keyword arguments:
number = 6
lang = "Python"
quote = "There are {} relational operators in {}.".format(number, lang)
print(quote)There are 6 relational operators in Python.Lastly, you can use the old %-formatting, which you will still find in older code:
number = 6
lang = "Python"
quote = "There are %s relational operators in %s." % (number, lang)
print(quote)There are 6 relational operators in Python.Add Leading Zeros or Thousands Separators
F-strings accept a format specifier after a colon. To pad a number with leading zeros to a minimum width, use 0 followed by the width:
number = 7
f"{number:03}"'007'The width is a minimum, so a value such as 1000 still produces four digits.
This is handy for filenames such as image-007.jpg, where the numbers need to sort correctly. The zfill() string method gives the same result and keeps the minus sign in front of the zeros:
str(-5).zfill(3)'-05'To group the digits of a large number, add a comma or an underscore to the format specifier:
f"{1234567:,}"'1,234,567'Convert an Integer to a Binary, Octal, or Hexadecimal String
The str() function always returns the decimal form of a number. Python has separate built-in functions for other bases:
print(bin(10))
print(oct(493))
print(hex(255))0b1010
0o755
0xffEach function adds a prefix that shows the base. If you need the digits without the prefix, use a format specifier instead. The b, o, and x types return binary, octal, and hexadecimal digits, and you can combine them with zero padding:
print(f"{255:x}")
print(f"{255:08b}")ff
11111111To parse these strings back into numbers, pass the base to int(), as shown in the guide on converting strings to integers
.
Convert a List of Integers to a String
The join() method combines a list of strings into a single string, but it raises an error when the list contains integers:
numbers = [1, 2, 3]
", ".join(numbers)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int foundConvert each item with map() before joining the list:
numbers = [1, 2, 3]
", ".join(map(str, numbers))'1, 2, 3'The map() function calls str() on every item, so join() receives strings and builds the result.
Conclusion
Use str() or an f-string to convert an integer to a string in Python, and add a format specifier when you need padding, separators, or a different base. To convert a string back to a number, see how to convert strings to integers in Python
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page