Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Convert a Python String to int
Integers are whole numbers. In other words, they have no fractional component. Two data types you can use to store an integer in Python are int
and str
. These types offer flexibility for working with integers in different circumstances. In this tutorial, you’ll learn how you can convert a Python string to an int
. You’ll also learn how to convert an int
to a string.
By the end of this tutorial, you’ll understand:
str
and int
int
int
to a stringLet’s get started!
Python Pit Stop: This tutorial is a quick and practical way to find the info you need, so you’ll be back to your project in no time!
Free Bonus: Click here to get a Python Cheat Sheet and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.
Representing Integers in PythonAn integer can be stored using different types. Two possible Python data types for representing an integer are:
For example, you can represent an integer using a string literal:
Here, Python understands you to mean that you want to store the integer 110
as a string. You can do the same with the integer data type:
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:
Notice that binary
and hexadecimal
use prefixes to identify the number system. All integer prefixes are in the form 0?
, in which you replace ?
with a character that refers to the number system:
Technical Detail: 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:
The string representation of an integer is more flexible because a string holds arbitrary text data:
Each of these strings represent the same integer.
Now that you have some foundational knowledge about how to represent integers using str
and int
, you’ll learn how to convert a Python string to an int
.
int
If you have a decimal integer represented as a string and you want to convert the Python string to an int
, then you just pass the string to int()
, which returns a decimal integer:
By default, int()
assumes that the string argument represents a decimal integer. If, however, you pass a hexadecimal string to int()
, then you’ll see a ValueError
:
The error message says that the string is not a valid decimal integer.
Note:
It’s important to recognize the difference between two types of failed results of passing a string to int()
:
ValueError
will occur when int()
doesn’t know how to parse the string using the provided base (10 by default).int()
does know how to parse the string, but not the way you expected.Here’s an example of a logical error:
In this example, you meant for the result to be 210, which is the decimal representation of the binary string. Unfortunately, because you didn’t specify that behavior, int()
assumed that the string was a decimal integer.
One good safeguard for this behavior is to always define your string representations using explicit bases:
Here, you get a ValueError
because int()
doesn’t know how to parse the binary string as a decimal integer.
When you pass a string to int()
, you can specify the number system that you’re using to represent the integer. The way to specify the number system is to use base
:
Now, int()
understands you are passing a hexadecimal string and expecting a decimal integer.
Technical Detail: The argument that you pass to base
is not limited to 2, 8, 10, and 16:
Great! Now that you’re comfortable with the ins and outs of converting a Python string to an int
, you’ll learn how to do the inverse operation.
int
to a String
In Python, you can convert a Python int
to a string using str()
:
By default, str()
behaves like int()
in that it results in a decimal representation:
In this example, str()
is smart enough to interpret the binary literal and convert it to a decimal string.
If you want a string to represent an integer in another number system, then you use a formatted string, such as an f-string (in Python 3.6+), and an option that specifies the base:
str
is a flexible way to represent an integer in a variety of different number systems.
Congratulations! You’ve learned so much about integers and how to represent and convert them between Python string and int
data types.
In this tutorial, you learned:
str
and int
to store integersint
int
to a stringNow that you know so much about str
and int
, you can learn more about representing numerical types using float()
, hex()
, oct()
, and bin()
!
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Convert a Python String to int
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4