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!

Converting String to Int in Python

Learn how to convert string to int in Python with this comprehensive guide. Understand the relationship between strings and integers, and discover how to perform this conversion using various methods. …


Updated May 22, 2023

Learn how to convert string to int in Python with this comprehensive guide. Understand the relationship between strings and integers, and discover how to perform this conversion using various methods.

Definition of the Concept

In Python, a string is a sequence of characters, such as words or sentences. On the other hand, an integer is a whole number, either positive, negative, or zero. When you have a string that represents a numerical value, like “123” or “-456”, and want to use it as an integer in your Python program, you need to convert the string to an int.

Step-by-Step Explanation

Converting a string to an int involves using a built-in function called int(). This function takes a string argument and returns its equivalent integer value. Here’s a step-by-step breakdown:

  1. Identify the String: Determine which string you want to convert to an int.
  2. Use the int() Function: Call the int() function, passing the string as an argument.
  3. Get the Integer Value: The int() function returns the integer equivalent of the input string.

Code Snippet 1: Basic Conversion

# Define a string representing a numerical value
num_str = "123"

# Convert the string to an int using the int() function
num_int = int(num_str)

print(num_int)  # Output: 123

In this example, we define a string num_str containing the digits “123”. We then use the int() function to convert this string to its integer equivalent. The resulting value is stored in the variable num_int.

Code Explanation

Let’s break down the code:

  • int(num_str): This line calls the int() function, passing the string num_str as an argument.
  • print(num_int): After converting the string to an int, we print its value using the built-in print() function.

Code Snippet 2: Handling Errors

# Define a non-numeric string
non_num_str = "hello"

try:
    num_int = int(non_num_str)
except ValueError as e:
    print(f"Error: {e}")

In this example, we define a string non_num_str that contains letters instead of digits. When we attempt to convert it to an int using the int() function, a ValueError exception is raised.

Code Explanation

Here’s how the code works:

  • try:: We try to execute the block of code inside the try clause.
  • num_int = int(non_num_str): Inside the try block, we attempt to convert the string non_num_str to an int using the int() function.
  • except ValueError as e:: If a ValueError exception occurs during the conversion (which is expected since the input string contains non-numeric characters), we catch it in the except block.
  • print(f"Error: {e}"): We print an error message indicating that the input string cannot be converted to an int.

Conclusion

Converting a string to an int in Python using the int() function is a straightforward process. By understanding how this conversion works and handling potential errors, you can effectively use strings as integers in your programs.

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

Intuit Mailchimp