How to convert an int to a string in Python?
What’s the proper way to convert an integer like 42 into a string in Python?
Example:
42 → "42"
I’m looking for clean and simple ways to perform int to string conversion in Python, especially when formatting or printing values.
The simplest and most Pythonic way I use is just str(). So if I have num = 42, I just do:
text = str(num)
It’s clean, readable, and works for any type of number.
I use it all the time when printing values or building strings dynamically, never had to look beyond it unless I needed specific formatting.
I agree with @prynka.chatterjee , but in my case, I was building strings like “User ID: 42” and found f-strings really handy:
user_id = 42
print(f"User ID: {user_id}")
Behind the scenes, it’s converting int to str, but it feels more natural when you’re composing output.
Plus, it’s faster and easier to read than using str() + concatenation, especially for logs or messages.
I come from older Python versions where f-strings didn’t exist yet, so I still use .format()
out of habit sometimes:
"Value is {}".format(42)
It does the same job, converts the int into a string for you.
These days, I try to shift to f-strings, but .format()
still works great, especially if you’re maintaining older codebases.