Python Programming
Integer Conversion
String Conversion
Coding Techniques
Python Data Types

Convert integer to string in Python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Turning an integer into a string is simple in Python, but the best approach depends on what you want the result to look like. Sometimes you only need the plain digits, and other times you want padding, separators, or a non-decimal representation such as hexadecimal.

The standard solution: str()

For ordinary conversion, use str():

python
1value = 123
2text = str(value)
3
4print(text)         # 123
5print(type(text))   # <class 'str'>

This works for negative numbers, zero, and very large integers:

python
print(str(-42))      # -42
print(str(0))        # 0
print(str(10**12))   # 1000000000000

If your only goal is to convert an int into a textual form, str() is the most direct and readable choice.

Building strings with f-strings

If the integer is part of a larger message, f-strings are usually cleaner than converting first and concatenating later:

python
count = 7
message = f"Processed {count} files"
print(message)

F-strings also make formatting easy:

python
1order_id = 42
2
3print(f"{order_id:05d}")   # 00042
4print(f"{1000000:,}")      # 1,000,000
5print(f"{255:x}")          # ff
6print(f"{255:b}")          # 11111111

That is especially useful for filenames, identifiers, and logs where the integer needs a specific appearance.

Using format() when formatting rules matter

Python's format() function gives the same formatting power without embedding the value directly into a string literal:

python
1value = 255
2
3print(format(value, "d"))   # 255
4print(format(value, "x"))   # ff
5print(format(value, "b"))   # 11111111
6print(format(value, "08d")) # 00000255

This is useful when the format specifier is dynamic or passed in from elsewhere:

python
spec = "06d"
value = 91
print(format(value, spec))  # 000091

Converting lists of integers

When you need to join multiple integers into one string, convert each item first:

python
numbers = [10, 20, 30]
result = ", ".join(str(n) for n in numbers)
print(result)  # 10, 20, 30

Trying to join integers directly raises an error because join() expects strings, not numbers.

This pattern shows up all the time in CSV generation, logs, and command construction.

Decimal versus other bases

An integer can be turned into more than one kind of string. Python includes helpers for common bases:

python
1value = 26
2
3print(str(value))  # 26
4print(bin(value))  # 0b11010
5print(oct(value))  # 0o32
6print(hex(value))  # 0x1a

If you want the base-specific text without the prefix:

python
print(format(value, "b"))  # 11010
print(format(value, "o"))  # 32
print(format(value, "x"))  # 1a

That distinction matters when the string is intended for display, serialization, or another system that expects a specific numeric format.

Common Pitfalls

The most common mistake is concatenating a string and an integer directly:

python
count = 5
# TypeError
# print("Count: " + count)

Fix it with str(count) or an f-string:

python
print("Count: " + str(count))
print(f"Count: {count}")

Another issue is assuming every number-to-string operation is just plain decimal conversion. If the output needs padding, separators, or hex formatting, str() alone is not enough.

Be careful when converting values that are not actually integers. str(True) returns "True", not "1", because True is displayed as a boolean representation. If you need numeric text, convert intentionally.

Finally, when formatting user-facing output, think about locale and presentation rules. The string form that works for logs may not be the right form for a UI or report.

A useful rule of thumb is simple: use str() for conversion, and use formatting tools only when the string has display requirements. That keeps ordinary code easy to read and makes special formatting explicit when it actually matters.

Summary

  • Use str() for plain integer-to-string conversion.
  • Use f-strings when the integer is part of a larger string or needs formatting.
  • Use format() when the formatting specifier is important or dynamic.
  • Convert each integer to text before using join().
  • Choose the right representation: decimal, padded decimal, binary, octal, or hexadecimal.

Course illustration
Course illustration

All Rights Reserved.