How to Convert Int to Str in Python: Every Method Explained

By Rahul Singh

Updated on Jun 04, 2026 | 10 min read | 3.74K+ views

Share:

Converting int to str in Python is a common task that allows you to transform numeric values into strings. This conversion is often needed when displaying numbers in messages, formatting output, creating file names, processing user input, or working with text-based data. Python provides several ways to perform this conversion, with the built-in `str()` function being the most widely used and beginner-friendly approach.

In this guide, you'll learn how to convert int to str in Python using different methods, including `str()`, f-strings, the `format()` method, and legacy formatting techniques. You'll also explore practical examples, advanced formatting options such as leading zeros, and common use cases that appear in real-world Python applications.

Transform your career with upGrad’s Data Science Course. Learn from industry experts, work on hands-on projects, and gain the skills top employer’s demand.

The Simplest Way to Convert Int to Str in Python

Python gives you a built-in function for this: str(). It is the most straightforward method, works in every version of Python, and handles almost every use case you will encounter as a beginner.

Using str() to Convert an Integer

Here is the basic syntax:

str(integer_value)

Let's see it in action:

age = 25
age_as_string = str(age)

print(age_as_string)        # Output: 25
print(type(age_as_string))  # Output: <class 'str'>

The str() function takes any integer and returns its string equivalent. Notice how type() confirms the conversion worked. This is something beginners should always check when troubleshooting type errors.

Also Read: 16+ Essential Python String Methods You Should Know

Why You Cannot Concatenate Directly

This is a mistake almost every beginner makes at least once:

score = 95
print("Your score is " + score)
# TypeError: can only concatenate str (not "int") to str

The fix is simple:

score = 95
print("Your score is " + str(score))  # Output: Your score is 95

Python is strictly typed when it comes to string operations. It does not automatically convert numbers to text during concatenation. You have to do it explicitly, and str() is the cleanest way.

When str() Is the Right Choice

  • Joining numbers with strings using the + operator
  • Checking or comparing the string representation of a number
  • Converting a number before writing it to a text file
  • Logging values where everything needs to be a string

str() is also safe to use with zero, negative numbers, and large integers. It will not raise an error or lose precision for standard int values.

Also Read: String Methods Python

Other Methods to Convert Int to Str in Python

str() is not the only tool available. Depending on what you need, Python offers several other ways to convert an integer to a string. Each method has its own strengths.

1. Using f-Strings (Python 3.6+)

f-strings are one of the most popular features added in Python 3.6. They let you embed variables directly inside a string, and Python handles the conversion automatically.

speed = 120
message = f"Current speed: {speed} km/h"
print(message)  # Output: Current speed: 120 km/h 

When you place an integer inside curly braces in an f-string, Python converts it to a string on the fly. You do not need to call str() separately. This makes code shorter and much easier to read.

2. Using format()

The format() method is the predecessor to f-strings. It uses {} as a placeholder and inserts values into the string.

items = 7
result = "You have {} items in your cart.".format(items)
print(result) # Output: You have 7 items in your cart.

You can use format() when you are working with Python 3.5 or earlier, or when you prefer to define a template string separately from the values.

Also Read: Understanding Python Data Types

3. Using % Formatting (Old-Style)

This is the oldest formatting style in Python, borrowed from C's printf. You will still see it in legacy codebases.

count = 42
text = "Count: %d" % count
print(text)  # Output: Count: 42

# Or convert directly to string representation:
text2 = "%s" % count
print(text2) # Output: 42 

The %d placeholder formats an integer as a decimal number. The %s placeholder converts the integer to its string form. For new code, f-strings or format() are generally preferred over %-style formatting.

Quick Comparison of All Methods

Method

Syntax Example

Python Version

Best For

str() str(42) All versions Simple, direct conversion
f-string f"{42}" 3.6+ Readable inline formatting
format() "{0}".format(42) 3.0+ Template-based formatting
% formatting "%s" % 42 All versions Legacy code only

Also Read: Mastering the Randint Python Function for Random Integer Generation

Advanced Int to Str Conversions in Python

Once you are comfortable with the basics, Python lets you go further. You can convert integers to binary, octal, or hexadecimal strings, add formatting like zero-padding, and control how numbers appear in output. These are tools you will need once you start working on real projects.

1. Converting to Binary, Octal, and Hexadecimal Strings

Python has built-in functions for converting integers to other number systems as strings.

number = 255

print(bin(number))   # Output: 0b11111111  (binary string)
print(oct(number))   # Output: 0o377       (octal string)
print(hex(number))   # Output: 0xff        (hexadecimal string) 

These functions return strings with a prefix (0b, 0o, 0x). If you want just the digits without the prefix, you can slice the string:

binary_clean = bin(255)[2:]  # '11111111'
hex_clean   = hex(255)[2:]  # 'ff'

2. Zero-Padding and Width Formatting

When displaying numbers in tables or logs, you often want consistent spacing. Python format specifiers handle this neatly.

# Zero-padded to 5 digits
print(f"{42:05d}")    # Output: 00042

# Right-aligned in a 10-character wide field
print(f"{42:>10}")    # Output:         42

# Using format() for the same result
print("{:05d}".format(42))  # Output: 00042

3. Using repr() vs str()

Both repr() and str() can convert integers to strings, and for simple integers they produce the same output. The difference matters more for complex objects.

x = 100
print(str(x))   # Output: 100
print(repr(x))  # Output: 100

For integers, use str(). repr() is meant for developer-facing output and is more relevant when you work with custom classes or debugging.

Also Read: Functions in Python: Definition, Types, Syntax, and Usage Explained

4. Converting a List of Integers to Strings

A common task is converting multiple integers at once. The map() function is the cleanest way to do this.

numbers = [1, 2, 3, 4, 5]

# Using map()
strings = list(map(str, numbers))
print(strings)  # Output: ['1', '2', '3', '4', '5']

# Using a list comprehension
strings2 = [str(n) for n in numbers]
print(strings2)  # Output: ['1', '2', '3', '4', '5']

Both approaches work. map() is slightly more concise; list comprehensions are often more readable for those new to Python.

Also Read: Python Built-in Modules: Supercharge Your Coding Today!

Common Errors When Converting Int to Str in Python

Even with simple conversions, mistakes happen. Knowing the common errors saves you time debugging. Here are the ones you are most likely to encounter.

TypeError: can only concatenate str (not int) to str

This is the most frequent error when working with int to str in Python. It happens when you forget to convert the integer before joining it with a string.

# Wrong
x = 10
print("Value: " + x)        # TypeError

# Right
print("Value: " + str(x))   # Output: Value: 10

Confusing int() and str()

Some beginners accidentally call int() when they want str(), or vice versa. int() converts a string to a number; str() does the opposite.

n = 50
wrong = int(n)   # Returns 50 as int (no change)
right = str(n)   # Returns '50' as a string

Also Read: Hash tables and Hash maps in Python

Working with None or Non-Integer Types

str() works on None and floats too, so it will not raise an error, but the result may not be what you expected.

print(str(None))   # Output: 'None'
print(str(3.14))   # Output: '3.14'  (float, not int)

If you want to ensure you are only converting genuine integers, use type checking with isinstance() before the conversion.

value = 42
if isinstance(value, int):
   result = str(value)
   print(result)  # Output: '42'

Quick Error Reference

Error

Cause

Fix

TypeError Concatenating int + str directly Wrap integer with str()
Wrong output Using int() instead of str() Use str() for conversion
Unexpected 'None' Passing None to str() Validate input type first
Lost precision Converting float with str() Use round() before str() if needed

Practical Use Cases for Int to Str Conversion in Python

Understanding the theory is one thing. Seeing where it actually shows up in real Python projects makes it stick. Here are scenarios where converting int to str in Python is not optional.

Building Dynamic Messages

username = "Priya"
score   = 480
level   = 7

message = f"Hey {username}, you reached Level {level} with {score} points!"
print(message)
# Output: Hey Priya, you reached Level 7 with 480 points!

Also Read: Python Classes and Objects [With Examples]

Writing Data to Text Files

results = [101, 202, 303]

with open("output.txt", "w") as f:
    for r in results:
       f.write(str(r) + "\n") # File write needs strings

Joining Numbers into a CSV-style String

ids = [1, 2, 3, 4]
csv_line = ",".join(str(i) for i in ids)
print(csv_line)  # Output: 1,2,3,4

The join() method only works on iterables of strings. Since ids contains integers, you must convert each one inline. The generator expression inside join() does exactly that.

Dictionary Keys and JSON Serialization

import json

data = {"user_id": 1001, "points": 250}
json_string = json.dumps(data)
print(json_string)
# Output: {"user_id": 1001, "points": 250}
# json.dumps handles int to str internally for values

Conclusion

Converting int to str in Python is a task you will do constantly throughout your programming journey. The good news is Python gives you multiple clean ways to do it. For most situations, str() is all you need. When you want readable inline formatting, f-strings are the modern standard. For legacy code or template-heavy logic, format() and %-style formatting still have their place.

If you want to strengthen your Python fundamentals and build towards a career in data science, or AI, upGrad offers structured Data Science learning paths with hands-on projects that take you from basics to job-ready skills.

Want personalized guidance on Python and Data Science and upskilling? Speak with an expert for a free 1:1 counselling session today.   

Frequently Asked Question (FAQs)

1. What is the difference between str() and repr() when converting an integer in Python?

For integers, both str() and repr() return the same output. The difference matters with complex objects. str() is intended for end-user-readable output, while repr() is meant for developer debugging and aims to be unambiguous. For simple int to str conversions, always use str().

2. Can I convert a negative integer to a string in Python?

Yes. str() handles negative integers without any issues. str(-99) returns '-99' as a string, including the minus sign. The same applies to f-strings and format(). There are no special steps needed for negative values.

3. How do I convert an integer to a string with leading zeros in Python?

Use a format specifier inside an f-string or format(). For example, f"{7:03d}" returns '007', padding the number with zeros to reach a total width of three characters. This is useful for serial numbers, timestamps, and display formatting.

4. What happens if I pass a float to str() instead of an int?

str() will convert the float as-is. str(3.14) returns '3.14', not '3'. If you want only the integer part as a string, convert to int first: str(int(3.14)) returns '3'. Be aware that int() truncates rather than rounds.

5. How do I convert an integer to a binary string in Python?

Use the bin() function. bin(10) returns '0b1010'. The '0b' prefix indicates it is a binary representation. To get just the digits without the prefix, use slicing: bin(10)[2:] returns '1010'. You can also use format specifiers: f"{10:b}" gives '1010'.

6. Is there a way to convert a list of integers to a list of strings in one line?

Yes. Use map(str, your_list) combined with list(). Example: list(map(str, [1, 2, 3])) returns ['1', '2', '3']. Alternatively, a list comprehension works: [str(n) for n in [1, 2, 3]]. Both approaches are clean and commonly used in Python.

7. Does Python automatically convert int to str in print()?

Yes, but only inside print() itself. Python calls str() on any non-string argument passed to print(). However, this automatic conversion does not apply to string concatenation using +. If you are building a string manually, you still need to convert with str() explicitly.

8. What is the fastest method to convert int to str in Python for large-scale data?

For large datasets, str() and f-strings perform comparably and are both very fast. map(str, list) is efficient when processing lists. In performance-critical loops processing millions of conversions, str() is slightly faster than f-strings due to lower overhead. For most applications, the difference is negligible.

9. Can I convert an integer to a hexadecimal string in Python?

Yes. Use hex(255) to get '0xff'. To strip the prefix, use hex(255)[2:] which gives 'ff'. For uppercase hex, use format(255, 'X') which returns 'FF'. f-strings also support this: f"{255:x}" gives 'ff' and f"{255:X}" gives 'FF'.

10. Why does Python raise a TypeError when I add an integer and a string?

Python uses strict typing for string operations. Unlike JavaScript, it does not implicitly convert types during concatenation. When you use the + operator, both sides must be the same type. If you try string + integer, Python raises a TypeError. Converting the integer to a string with str() before the + operator fixes this.

11. How do I check if a value is already a string before converting?

Use isinstance(value, str) to check. If it returns True, the value is already a string and no conversion is needed. If False, you can safely call str(value). This pattern is useful in functions that accept both strings and integers as input, preventing redundant conversions and keeping code robust.

Rahul Singh

49 articles published

Rahul Singh is an Associate Content Writer at upGrad, with a strong interest in Data Science, Machine Learning, and Artificial Intelligence. He combines technical development skills with data-driven s...

Start Your Career in Data Science Today