Python How to Convert Integer to String Easily
To convert an integer to a string in Python, use the
str() function like this: str(your_integer).Examples
Input123
Output"123"
Input0
Output"0"
Input-456
Output"-456"
How to Think About It
To convert an integer to a string, think of changing a number into text form so you can use it where words are needed. You do this by applying the
str() function, which takes the number and returns its text version.Algorithm
1
Get the integer value you want to convert.2
Apply the <code>str()</code> function to the integer.3
Return or use the resulting string.Code
python
number = 123 string_version = str(number) print(string_version) print(type(string_version))
Output
123
<class 'str'>
Dry Run
Let's trace converting the integer 123 to a string.
1
Start with integer
number = 123
2
Convert using str()
string_version = str(number) # string_version is now "123"
3
Print result
print(string_version) outputs 123 as text
| Step | Variable | Value |
|---|---|---|
| 1 | number | 123 (int) |
| 2 | string_version | "123" (str) |
| 3 | print output | 123 |
Why This Works
Step 1: Using str() function
The str() function takes any value and returns its string form.
Step 2: Integer to string conversion
When you pass an integer to str(), it creates a new string that looks like the number.
Step 3: Result is a string type
The output is a string, so you can use it where text is needed, like printing or joining with other strings.
Alternative Approaches
Using f-string
python
number = 123 string_version = f"{number}" print(string_version)
This method is readable and useful when combining numbers with text, but internally it also converts the number to string.
Using format() method
python
number = 123 string_version = "{}".format(number) print(string_version)
This is an older method for string formatting, still valid but less concise than f-strings.
Complexity: O(1) time, O(n) space
Time Complexity
Converting an integer to a string takes constant time because it only processes the digits once.
Space Complexity
The space depends on the number of digits in the integer, as the string stores each digit as a character.
Which Approach is Fastest?
Using str() is the fastest and most straightforward; f-strings and format() add slight overhead for formatting.
| Approach | Time | Space | Best For |
|---|---|---|---|
| str() | O(1) | O(n) | Simple direct conversion |
| f-string | O(1) | O(n) | Combining numbers with text |
| format() | O(1) | O(n) | Older formatting style |
Use
str() for the simplest and most direct integer to string conversion.Trying to concatenate an integer directly with a string without converting it first causes errors.