Convert Strings to Numbers and Numbers to Strings in Python
Last Updated :
23 Jul, 2025
Converting strings to numbers and numbers to strings is a common task in Python. In this article, we’ll explore simple ways to convert between strings and numbers .
Using int() and str()
int() and str() functions in Python are commonly used for converting data types. The int() function changes a string or number into an integer, while the str() function converts a number or object into a string, making it easy to work with different data formats.
Python
# Convert `s1` to integer
s1 = "123"
res = int(s1)
print(type(res))
# Convert `a` to string
a = 123
res= str(a)
print(type(res))
Output<class 'int'>
<class 'str'>
Let's understand different method to convert string to number and number to string.
Using eval()
eval() function evaluates a string as a Python expression and returns the result. While it can be used to convert strings to numbers, such as integers or floats .
Python
# Convert `s1` to Integer
s1 = "123"
res = int(s1)
print(type(res))
# Convert `s2` to Float
s2 = "123.45"
res = float(s2)
print(type(res))
Output<class 'int'>
<class 'float'>
Using ast.literal_eval()
ast.literal_eval() function safely evaluates a string containing a Python literal or container display, such as strings, numbers, tuples, lists, dicts, sets, booleans, and None.
Python
import ast
# String to Integer
s1 = "123"
res = ast.literal_eval(s1)
print(type(res))
# String to Float
s2 = "123.45"
res = ast.literal_eval(s2)
print(type(res))
Output<class 'int'>
<class 'float'>
Explanation:
- ast.literal_eval(s1):This takes the string "123" and treats it as a Python number. Since "123" is a valid integer, it returns the integer 123.
- ast.literal_eval(s2):This takes the string "123.45" and treats it as a valid float, safely returning the value 123.45.
format()is a efficient way to format strings, especially when we need to include numbers within the string or apply specific formatting like decimal precision or comma separators.
Python
# Convert int to String
a = 123
res = "{}".format(a)
print(type(res))
# Convert float to String
b = 123.45
res = "{:.2f}".format(b)
print(type(res))
Output<class 'str'>
<class 'str'>
Explanation:
"{}".format(a): This is used to format and insert the value of a (123) into the string placeholder {}. This converts the integer to its string representation, "123"."{:.2f}" : This ensures that the float is displayed with exactly two decimal places, even if the original value has more or fewer decimal places.
Using f-strings
F-strings is easy way to turn numbers (like integers and floats) into strings in Python. We can easily format numbers, such as displaying a specific number of decimal places or embedding numbers directly in the text.
Python
# Integer to String
a = 123
res = f"{a}"
print(type(res))
# Float to String
b = 123.45
res = f"{b:.2f}"
print(type(res))
Output<class 'str'>
<class 'str'>
Explanation:
f"{a}": This simply converts the integer to a string.f"{b:.2f}": This converts the float to a string, ensuring it always has 2 decimal places, even if the number has fewer.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice