TechieClues
Updated date Mar 18, 2024
In this article, we will discuss different ways to convert a string to an integer in Python, including built-in functions. We will also provide sample programs to illustrate each method.

## Converting a String to an Integer in Python

Python provides two built-in functions to convert a string to an integer: int() and float(). The int() function is used to convert a string to an integer, while the float() function is used to convert a string to a floating-point number. In this article, we will focus on the int() function.

The syntax of the int() function is as follows:

int(string, base)

where:

• string: The string to be converted to an integer.
• base (optional): The base of the number in the string. This can be any integer from 2 to 36. If not specified, the default value is 10.

If the string contains only digits, the base parameter can be omitted. If the string contains characters other than digits, the base parameter must be specified.

Here is an example of how to use the int() function to convert a string to an integer:

number_str = "123"
number_int = int(number_str)
print(number_int)

### Output:

123

In the above example, we first define a string number_str that contains the value "123". We then use the int() function to convert the string to an integer and store the result in the variable number_int. Finally, we print the value of number_int.

If the string contains characters other than digits, the int() function will raise a ValueError. For example:

number_str = "abc"
number_int = int(number_str)
print(number_int)

### Output:

ValueError: invalid literal for int() with base 10: 'abc'

In this case, the int() function raises a ValueError because the string "abc" cannot be converted to an integer.

If the string contains a number in a different base, you can specify the base parameter to convert it to an integer. For example:

number_str = "1010"
number_int = int(number_str, 2)
print(number_int)

### Output:

10

In this example, the string "1010" represents a binary number. We use the int() function with the base parameter set to 2 to convert the string to an integer. The result is the decimal number 10.

Here is a program that demonstrates different ways to convert a string to an integer in Python:

# Convert a string to an integer using the int() function
number_str = "123"
number_int = int(number_str)
print(number_int)

# Convert a string to an integer with a different base
binary_str = "1010"
binary_int = int(binary_str, 2)
print(binary_int)

# Handle errors when converting a string to an integer
invalid_str = "abc"
try:
invalid_int = int(invalid_str)
except ValueError as e:
print("Error:", e)

### Output:

123
10
26
Error: invalid literal for int() with base 10: 'abc'

In this program, we first convert a string containing the value "123" to an integer using the int() function. We then convert a binary string "1010" to an integer using the int() function with a base of 2. We also convert a hexadecimal string "1a" to an integer using the int() function with a base of 16.