How to Convert an Int to a String in Python
str(number), for example str(42) returns "42". For formatting such as padding or commas, use an f-string like f"{n:,}".Numbers and text are different types in Python, so you cannot join them directly with +. To build a message, a file name or an ID, you first turn the integer into a string. Type casting with str() is the shortest route, and f-strings do the conversion for you while you format.
Which tool is best depends on whether you only need the digits or also want control over the look of the text.
| Syntax | str(number) or f"{number}" |
|---|---|
| Returns | A new str |
| Fails | Never for integers (any int can be converted) |
| Opposite | int(text) |
Convert with str()
str() returns the decimal text of the number. The original integer is unchanged.
items = 3
message = "Items in cart: " + str(items)
print(message)
print(type(str(items)).__name__)Convert with f-strings and format()
An f-string converts the value and lets you add a format specifier after a colon. This is usually the cleanest way to put numbers inside sentences.
n = 1234567
print(f"{n}")
print(f"{n:,}")
print(f"{7:03d}")
print(format(10, "b"))Which method should you pick?
| Method | Example | Result | Best for |
|---|---|---|---|
str() | str(42) | '42' | Plain conversion |
| f-string | f"{42}" | '42' | Text with several values |
| Thousands separator | f"{1234567:,}" | '1,234,567' | Display to people |
| Zero padding | f"{7:03d}" | '007' | IDs, file names |
| Binary / hex | format(255, "x") | 'ff' | Other bases |
Why you cannot join a string and an int with +
"Total: " + 5 fails because Python will not guess how to mix types. Either convert with str(), use an f-string, or pass several arguments to print(), which converts for you. The error is a TypeError. To go the other way, see converting a string to an int.
Converting many integers at once
To turn a whole list of numbers into text, use map(str, numbers) or a comprehension, then join them into one string.
Examples you can run
Press Run, then edit the code and run it again.
Build a message from an integer
Both approaches produce the same text.
count = 5
print("Items: " + str(count))
print(f"Items: {count}")Expected outputItems: 5
Items: 5
Format for display and file names
Commas for readability and zero padding for sortable names.
revenue = 2500000
invoice = 42
print(f"Revenue: {revenue:,}")
print(f"invoice_{invoice:05d}.pdf")Expected outputRevenue: 2,500,000
invoice_00042.pdf
Edge case: negative numbers, zero and digit access
The minus sign is part of the string. Converting also lets you work with individual digits.
print(str(-15))
print(str(0))
n = 9047
digits = [int(d) for d in str(n)]
print(digits)
print(sum(digits))Expected output-15
0
[9, 0, 4, 7]
20
Common mistakes
Concatenating text and a number
✗ Wrong
age = 30
print("Age: " + age)✓ Right
age = 30
print("Age: " + str(age))Convert the number first with str() or use an f-string.
Expecting str() to change the variable
✗ Wrong
n = 10
str(n)
print(n + 1)✓ Right
n = 10
text = str(n)
print(text + "1")Assign the result: n = str(n), or keep a separate variable.
Using len() on an integer
✗ Wrong
print(len(12345))✓ Right
print(len(str(12345)))Convert to a string first, then count characters.
How to Convert an Int to a String in Python: questions people ask
How do I convert an int to a string in Python?
Use str(number), for example str(100) gives "100". An f-string such as f"{number}" does the same.
How do I convert an int to a string with leading zeros?
Use a format specifier: f"{7:03d}" gives "007", or str(7).zfill(3).
Is there a size limit when converting a huge int to str?
Yes, from Python 3.11 a decimal conversion of more than 4300 digits raises ValueError unless you raise the limit with sys.set_int_max_str_digits(). Normal numbers are never affected.
Keep learning
Step-by-step lessons:
Python CastingPython f-StringsPython Numbers
Related:
How to Convert a String to an Int in PythonHow to Convert a List to a String in PythonHow to Convert a Float to an Int in Python
More in converting data:
How to Convert a String to an Int in PythonHow to Convert a List to a String in PythonHow to Convert a String to a List in PythonHow to Convert a String to a Float in PythonHow to Convert a Float to an Int in PythonHow to Convert a List to a Dictionary in PythonHow to Convert a Tuple to a List in PythonHow to Convert a String to a Datetime in Python See all →