Python - Convert Int to String

The `str()` function is Python's built-in type converter that transforms any integer into its string representation. This is the most straightforward approach for simple conversions.

Key Insights

  • Python provides three primary methods for integer-to-string conversion: str(), f-strings, and format(), each optimized for different use cases and performance requirements
  • String formatting with format specifiers enables precise control over number representation including padding, alignment, and base conversion (binary, octal, hexadecimal)
  • Type conversion handling becomes critical in production systems where implicit conversions can cause bugs—explicit conversion with error handling prevents runtime failures

Basic Conversion Methods

The str() function is Python’s built-in type converter that transforms any integer into its string representation. This is the most straightforward approach for simple conversions.

number = 42
string_number = str(number)
print(type(string_number))  # <class 'str'>
print(string_number)        # "42"

# Works with negative numbers
negative = -100
print(str(negative))  # "-100"

# Works with large integers
large_number = 1234567890123456789
print(str(large_number))  # "1234567890123456789"

For string interpolation, f-strings (formatted string literals) provide cleaner syntax and better performance than concatenation:

count = 150
message = f"Processing {count} records"
print(message)  # "Processing 150 records"

# Multiple values
users = 1250
active = 847
status = f"System has {users} total users, {active} active"
print(status)

The format() method offers more explicit control when you need consistent formatting across multiple conversions:

value = 99
formatted = "Item count: {}".format(value)
print(formatted)  # "Item count: 99"

# Multiple placeholders
x, y = 10, 20
result = "Coordinates: ({}, {})".format(x, y)
print(result)  # "Coordinates: (10, 20)"

Number Base Conversions

Converting integers to different bases is common when working with low-level operations, debugging, or displaying technical data. Python provides built-in functions and format specifiers for this.

decimal = 255

# Binary (base 2)
binary = bin(decimal)
print(binary)  # "0b11111111"
print(bin(decimal)[2:])  # "11111111" (without prefix)

# Octal (base 8)
octal = oct(decimal)
print(octal)  # "0o377"

# Hexadecimal (base 16)
hexadecimal = hex(decimal)
print(hexadecimal)  # "0xff"
print(hex(decimal)[2:].upper())  # "FF"

Using format specifiers provides more control over the output:

num = 255

# Format specifiers
print(f"{num:b}")   # "11111111" (binary, no prefix)
print(f"{num:o}")   # "377" (octal, no prefix)
print(f"{num:x}")   # "ff" (hex lowercase)
print(f"{num:X}")   # "FF" (hex uppercase)
print(f"{num:#x}")  # "0xff" (hex with prefix)

# Custom base conversion function
def int_to_base(number, base):
    if base < 2 or base > 36:
        raise ValueError("Base must be between 2 and 36")
    
    if number == 0:
        return "0"
    
    digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = []
    negative = number < 0
    number = abs(number)
    
    while number:
        result.append(digits[number % base])
        number //= base
    
    if negative:
        result.append('-')
    
    return ''.join(reversed(result))

print(int_to_base(255, 2))   # "11111111"
print(int_to_base(255, 16))  # "FF"
print(int_to_base(100, 5))   # "400"

Formatting and Padding

Production applications often require specific string formatting for display, alignment, or fixed-width output. Format specifiers handle padding, alignment, and sign display.

# Zero-padding
number = 42
padded = f"{number:05d}"
print(padded)  # "00042"

# Right alignment with spaces
aligned = f"{number:>10}"
print(f"[{aligned}]")  # "[        42]"

# Left alignment
left_aligned = f"{number:<10}"
print(f"[{left_aligned}]")  # "[42        ]"

# Center alignment
centered = f"{number:^10}"
print(f"[{centered}]")  # "[    42    ]"

# Custom fill character
custom = f"{number:*>10}"
print(custom)  # "********42"

For financial or scientific applications, sign handling and thousand separators are essential:

positive = 1234567
negative = -1234567

# Always show sign
print(f"{positive:+d}")   # "+1234567"
print(f"{negative:+d}")   # "-1234567"

# Space for positive, minus for negative
print(f"{positive: d}")   # " 1234567"
print(f"{negative: d}")   # "-1234567"

# Thousand separators
print(f"{positive:,}")    # "1,234,567"
print(f"{positive:_}")    # "1_234_567"

# Combined formatting
amount = 1234567
formatted_amount = f"{amount:>15,}"
print(f"Total: {formatted_amount}")  # "Total:       1,234,567"

Error Handling and Type Safety

In production code, assuming integer types without validation leads to runtime errors. Implement explicit type checking and conversion with proper error handling.

def safe_int_to_string(value):
    """Safely convert integer to string with type checking."""
    if not isinstance(value, int):
        raise TypeError(f"Expected int, got {type(value).__name__}")
    return str(value)

# Usage
try:
    result = safe_int_to_string(42)
    print(result)  # "42"
    
    result = safe_int_to_string("42")  # Raises TypeError
except TypeError as e:
    print(f"Conversion error: {e}")

When processing user input or external data, handle conversion failures gracefully:

def convert_to_string_safe(value, default="0"):
    """Convert value to string, handling various input types."""
    try:
        if isinstance(value, int):
            return str(value)
        elif isinstance(value, str):
            # Validate it's a numeric string
            int(value)  # Will raise ValueError if invalid
            return value
        elif isinstance(value, float):
            return str(int(value))
        else:
            return default
    except (ValueError, TypeError):
        return default

# Test cases
print(convert_to_string_safe(42))           # "42"
print(convert_to_string_safe("42"))         # "42"
print(convert_to_string_safe(42.7))         # "42"
print(convert_to_string_safe("invalid"))    # "0"
print(convert_to_string_safe(None))         # "0"

Performance Considerations

For high-volume conversions, method selection impacts performance. Benchmark different approaches for your specific use case:

import timeit

def benchmark_conversions():
    setup = "number = 12345"
    
    # str() function
    str_time = timeit.timeit('str(number)', setup=setup, number=1000000)
    
    # f-string
    fstring_time = timeit.timeit('f"{number}"', setup=setup, number=1000000)
    
    # format()
    format_time = timeit.timeit('"{0}".format(number)', setup=setup, number=1000000)
    
    # % formatting
    percent_time = timeit.timeit('"%d" % number', setup=setup, number=1000000)
    
    print(f"str():        {str_time:.4f}s")
    print(f"f-string:     {fstring_time:.4f}s")
    print(f"format():     {format_time:.4f}s")
    print(f"% formatting: {percent_time:.4f}s")

benchmark_conversions()

For bulk conversions, list comprehensions with str() provide optimal performance:

# Convert list of integers efficiently
numbers = list(range(10000))

# Efficient approach
string_numbers = [str(n) for n in numbers]

# Less efficient (creates intermediate strings)
string_numbers_slow = list(map(lambda x: f"{x}", numbers))

Use str() for simple conversions, f-strings for readability in string interpolation, and format specifiers when you need precise control over output formatting. Always validate input types in production code to prevent silent failures.

Liked this? There's more.

Every week: one practical technique, explained simply, with code you can use immediately.