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:
Looks like you're browsing from the
United StatesSome programs may not be available in your location
Some programs may not be available in your location
Switch to upGrad USAll courses
Certifications
More
By Rahul Singh
Updated on Jun 04, 2026 | 10 min read | 3.74K+ views
Share:
Table of Contents
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.
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.
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
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.
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
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.
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.
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
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.
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
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.
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'
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
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
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!
Even with simple conversions, mistakes happen. Knowing the common errors saves you time debugging. Here are the ones you are most likely to encounter.
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
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
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'
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 |
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.
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]
results = [101, 202, 303]
with open("output.txt", "w") as f:
for r in results:
f.write(str(r) + "\n") # File write needs strings
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.
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
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.
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().
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.
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.
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.
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'.
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.
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.
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.
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'.
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.
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.
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