Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How to Convert a String to an Int in Python

Learn how to convert a string to an integer in Python using the int() function. This article provides a detailed explanation, code snippets, and step-by-step instructions for converting strings to int …


Updated June 2, 2023

Learn how to convert a string to an integer in Python using the int() function. This article provides a detailed explanation, code snippets, and step-by-step instructions for converting strings to integers.

Definition of the Concept

In Python programming, converting a string to an integer is a common task that involves parsing a string representation of a number into its actual numerical value. This process is essential in various applications, such as data processing, scientific computing, and web development.

Step-by-Step Explanation

To convert a string to an integer in Python, you can use the built-in int() function. Here’s how it works:

1. Understanding the int() Function

The int() function takes two parameters: the string to be converted and an optional base parameter that specifies the number base of the input string.

int(string, [base])
  • string: The string representation of a number.
  • [base]: Optional. Specifies the number base of the input string (default is 10).

2. Converting a String to an Int

To convert a string to an integer using the int() function, you can pass the string as the first argument and omit the second argument (base) if it’s decimal.

# Define the string
string = "123"

# Convert the string to an int
number = int(string)

print(number)  # Output: 123

Handling Non-Decimal Strings

When dealing with non-decimal strings, you need to specify the correct base using the second parameter of the int() function.

# Define a hexadecimal string
hex_string = "1A"

# Convert the hexadecimal string to an int
hex_number = int(hex_string, 16)

print(hex_number)  # Output: 26

# Define an octal string
oct_string = "12"

# Convert the octal string to an int
oct_number = int(oct_string, 8)

print(oct_number)  # Output: 10

Error Handling

The int() function raises a ValueError when it encounters an invalid input.

try:
    number = int("123abc")
except ValueError as e:
    print(e)  # Output: invalid literal for int() with base 10: '123abc'

By following this step-by-step guide, you can master the art of converting strings to integers in Python using the powerful int() function.


Additional Resources

If you’re interested in learning more about working with numbers and strings in Python, I recommend checking out my comprehensive course on Python programming. The course covers a wide range of topics, including:

  • Advanced data types
  • String manipulation
  • Number processing
  • Regular expressions
  • File input/output

You can find the course details on my website.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp