How to Convert Integer to String in Python

What you’ll build or solve

You’ll convert an integer to a string so you can concatenate it, format messages, or write it to text output.

When this approach works best

Converting an integer to a string works well when you:

  • Build messages for users or logs, like User 42 signed in.
  • Create filenames or IDs, like report_2026_02_16.txt.
  • Join numbers into text output, like turning [1, 2, 3] into "1,2,3".

Skip this approach if you need to do math next. Keep the value as an integer until the final display or export step.

Prerequisites

  • Python 3 installed
  • You know what an integer and a string are

Step-by-step instructions

1) Convert an integer with str()

str() is the standard way to convert an integer to a string.

count=42
text=str(count)

print(text)
print(type(text))

What to look for: After conversion, the value prints the same, but the type changes to str.


2) Use f-strings for readable formatting

When you’re building a message, f-strings often read better than manual concatenation. Python converts the integer for you.

user_id=42
message=f"User{user_id} signed in"

print(message)

Option A (most common): f-string

Option B: format() for older code

user_id=42
message="User {} signed in".format(user_id)

print(message)

What to look for: f-strings work in Python 3.6+. If you’re on an older version, use format().


3) Convert while joining or concatenating

If you need to combine an integer with other strings, convert first or use formatting.

page=3
filename="page_"+str(page)+".txt"

print(filename)

For lists of integers, convert each item before joining.

nums= [10,25,30]
text=", ".join(map(str,nums))

print(text)

What to look for: join() only accepts strings, so map(str, nums) prevents TypeError.


Examples you can copy

Example 1: Build a filename with a number

report_num=7
filename=f"report_{report_num}.txt"
print(filename)

Example 2: Convert an integer for JSON output

JSON requires strings when you want the value stored as text.

importjson

user_id=42
data= {"user_id":str(user_id)}

print(json.dumps(data))

Example 3: Convert command-line input back and forth

You often parse strings into ints, then format them back to strings.

raw="42"
n=int(raw)
message=f"Parsed number:{n}"

print(message)

Common mistakes and how to fix them

Mistake 1: Concatenating a string and an int

What you might do:

count=3
text="Count: "+count

Why it breaks: + only concatenates strings, so Python raises TypeError.

Correct approach:

count=3
text="Count: "+str(count)

Or use formatting:

count=3
text=f"Count:{count}"

Mistake 2: Joining integers without converting

What you might do:

nums= [1,2,3]
text=", ".join(nums)

Why it breaks: join() requires strings, not integers.

Correct approach:

nums= [1,2,3]
text=", ".join(map(str,nums))

Mistake 3: Converting too early and breaking math

What you might do:

a=str(10)
b=str(5)
result=a+b

Why it breaks: String addition concatenates, so you get "105" instead of 15.

Correct approach:

a=10
b=5
result=a+b
text=str(result)

Troubleshooting

If you see TypeError: can only concatenate str (not "int") to str, wrap the int in str(...) or use an f-string.

If you see TypeError: sequence item 0: expected str instance, convert items before join(), for example map(str, nums).

If numbers look wrong after combining, check if you converted to a string before finishing arithmetic.

If you get unexpected spaces or punctuation, check the separator you used in join(), such as ", " vs ",".


Quick recap

  • Use str(n) to convert an integer to a string.
  • Use f-strings like f"Value: {n}" for readable text.
  • Convert items before joining: ", ".join(map(str, nums)).
  • Keep values as integers for math, then convert at the final display step.
  • Fix type errors by converting ints before concatenation or join().