Submitting the form below will ensure a prompt response from us.
When working with user input, files, APIs, or databases, developers frequently encounter numbers stored as strings. Before performing calculations, these strings must be converted into integers.
For example:
"100" → 100
This process is known as string-to-integer conversion.
So, how to convert Python Convert String to Int?
Many data sources return values as strings.
Examples include:
To perform mathematical operations, Python requires numeric data types such as integers.
The simplest and most common way to convert python convert string to int built-in int() function.
number_string = "123"
number = int(number_string)
print(number)
print(type(number))
123
<class 'int'>
print(type(number))
The int() function converts the string “123” into the integer 123.
int(value)
Where:
Example:
age = int("25")
print(age)
Output:
25
A common use case involves user input.
Example:
age = input("Enter your age: ")
age = int(age)
print("Next year you will be:", age + 1)
Since input() always returns a string, conversion is necessary before performing arithmetic operations.
The int() function also handles negative values.
Example:
num = int("-50")
print(num)
Output:
-50
Python automatically ignores surrounding spaces.
Example:
num = int(" 42 ")
print(num)
Output:
42
Not all strings can be converted into integers.
Example:
num = int("hello")
This produces:
ValueError: invalid literal for int()
You Might Also Like:
Understanding and Fixing “TypeError: ‘Type’ Object is Not Subscriptable” in Python
To prevent application crashes, use a try-except block.
Example:
try:
num = int("hello")
print(num)
except ValueError:
print("Invalid number format")
Output:
Invalid number format
This is the recommended approach when processing user input.
The int() function can also convert numbers from binary, octal, or hexadecimal formats.
num = int("1010", 2)
print(num)
Output:
10
num = int("FF", 16)
print(num)
Output:
255
| String Value | Conversion Method | Result |
|---|---|---|
| “100” | int(“100”) | 100 |
| “-25” | int(“-25”) | -25 |
| “0” | int(“0”) | 0 |
| ” 50 “ | int(” 50 “) | 50 |
| “1010” (Base 2) | int(“1010”, 2) | 10 |
| “FF” (Base 16) | int(“FF”, 16) | 255 |
Many APIs return numbers as strings.
Example:
api_response = {
"user_id": "1001",
"score": "85"
}
user_id = int(api_response["user_id"])
score = int(api_response["score"])
print(user_id + 1)
print(score + 5)
Output:
1002
90
Sometimes numbers contain decimals.
Example:
value = "10.5"
number = int(float(value))
print(number)
Output:
10
Notice that converting from float to int removes the decimal portion.
age = input("Age: ")
print(age + 1)
This causes an error because age is a string.
int("abc")
Results in a ValueError.
Always validate external data before conversion.
The int() function is highly optimized and suitable for:
For most use cases, no alternative method is necessary.
def safe_convert(value):
try:
return int(value)
except ValueError:
return None
print(safe_convert("100"))
print(safe_convert("abc"))
Output:
100
None
This approach improves code reliability.
Build Better Applications
Improve your coding skills with modern Python development practices.
Converting a string to an integer is one of the most common operations in Python programming. The built-in int() function provides a simple and efficient way to perform this conversion.
By using proper validation and exception handling, developers can safely process user input, API responses, files, and database values without unexpected errors.
Whether you’re building web applications, automation scripts, data pipelines, or machine learning solutions, understanding Python string-to-int conversion is an essential programming skill.