Skip to content

How to Easily Convert a String to an Integer

[

How to Convert a Python String to int

by Alex Ronquillo

Representing Integers in Python

An integer can be stored using different types. Two possible Python data types for representing an integer are str and int. For example, you can represent an integer using a string literal:

s = "110"

Here, Python understands that you want to store the integer 110 as a string. You can do the same with the integer data type:

i = 110

It’s important to consider what you specifically mean by "110" and 110 in the examples above. As a human who has used the decimal number system for your whole life, it may be obvious that you mean the number one hundred and ten. However, there are several other number systems, such as binary and hexadecimal, which use different bases to represent an integer.

For example, you can represent the number one hundred and ten in binary and hexadecimal as 1101110 and 6e respectively.

You can also represent your integers with other number systems in Python using the str and int data types:

binary = 0b1010
hexadecimal = "0xa"

Notice that binary and hexadecimal use prefixes to identify the number system. All integer prefixes are in the form 0?, where ? is replaced with a character that refers to the number system:

  • b: binary (base 2)
  • o: octal (base 8)
  • d: decimal (base 10)
  • x: hexadecimal (base 16)

The prefix is not required in either an integer or string representation when it can be inferred.

int assumes the literal integer to be decimal:

decimal = 303
hexadecimal_with_prefix = 0x12F

The string representation of an integer is more flexible because a string can hold arbitrary text data:

decimal = "303"
hexadecimal_with_prefix = "0x12F"

Converting a Python String to an int

To convert a Python string to an int, you can use the int() function. This function takes in a string as an argument and returns an integer representation of that string. If the string cannot be converted to an integer, a ValueError will be raised. Here’s an example:

string_num = "1234"
int_num = int(string_num)
print(type(int_num)) # Output: <class 'int'>
print(int_num) # Output: 1234

In the above code, the int() function is used to convert the string "1234" to the integer 1234. The type() function is used to verify that the output is indeed an int, and the print() function is used to display the integer value.

It’s important to note that if the string contains any non-numeric characters, the int() function will raise a ValueError. For example:

string_num = "12a34"
int_num = int(string_num)

The above code will raise a ValueError since the string "12a34" cannot be converted to an integer.

Converting a Python int to a String

To convert a Python int to a string, you can use the str() function. This function takes in an integer as an argument and returns a string representation of that integer. Here’s an example:

int_num = 1234
string_num = str(int_num)
print(type(string_num)) # Output: <class 'str'>
print(string_num) # Output: "1234"

In the above code, the str() function is used to convert the integer 1234 to the string "1234". The type() function is used to verify that the output is indeed a string, and the print() function is used to display the string value.

Conclusion

In this tutorial, you learned how to represent integers in Python using the str and int data types. You also learned how to convert a Python string to an integer using the int() function and how to convert a Python int to a string using the str() function.

By understanding these conversion techniques, you can manipulate and work with integers in Python based on your specific needs.