How to Convert a String to int in Python?

In this python tutorial, you will learn different ways to convert a string to int.

Table Of Contents

We know that a string is a set of characters and int stands for an integer which is a numeric value. So let’s see different ways to convert a string to an integer in python.

Convert a string to int using int() method

We can directly use the int() method to convert string to integer. After that, it is possible to check whether the string is converted to an integer or not using the type() method.

Syntax:

Advertisements
int(input_str)

Parameter

It takes input_str (string) as a parameter and returns an int value.

Example 1:

In this example, we will convert the string “9087” to an integer.

# Consider the string
my_str="9087"

print("String: ", my_str)

# Display the datatype
print("Actual Datatype: ", type(my_str))

# Convert to integer
converted = int(my_str)

print("Integer: ", converted)

# Display the datatype
print("Modified Datatype: ", type(converted))

Output:

String:  9087
Actual Datatype:  <class 'str'>
Integer:  9087
Modified Datatype:  <class 'int'>

Previously it belongs to the str class. After converting to an integer, the class is int.

Example 2:

In this example, we will convert the string “6” to an integer.

# Consider the string
my_str = "6"

print("String: ", my_str)

# Display the datatype
print("Actual Datatype: ", type(my_str))

# Convert to integer
converted=int(my_str)

print("Integer: ",converted)

# Display the datatype
print("Modified Datatype: ",type(converted))

Output:

String:  6
Actual Datatype:  <class 'str'>

Integer:  6
Modified Datatype:  <class 'int'>

Previously it belongs to the str class. After converting to an integer, the class is int.

Convert a Binary string to an int

In this case, we will consider a string that has 0’s and 1’s. Now we will convert that binary string to an integer using int().

Syntax:

int(input_binary_str, 2)

where input_binary_str is the binary string, and 2 represents the binary base.

Example:

In this example, we will convert the binary string “10100010” to an integer.

# Consider the binary string
my_str="10100010"

print(my_str)

# Display the datatype
print("Actual Datatype: ", type(my_str))

# Convert to integer
converted=int(my_str, 2)

print("Integer: ", converted)

# Display the datatype
print("Modified Datatype: ", type(converted))

Output:

10100010
Actual Datatype:  <class 'str'>

Modified Datatype:  <class 'int'>
Integer:  162

Previously it belongs to the str class. After converting to an integer, the class is int.

Convert a Hexa decimal string to int

In this case, we will consider a hexadecimal string. Now we will convert that hexadecimal string to an integer using int() by specifying the base value as 16. Where, 16 represents the hexadecimal base.

Syntax:

int(input_hexadecimal_str,base=16)

Where input_hexadecimal_str is the hexadecimal string.

Example 1:

In this example, we will convert the hexadecimal string “0x12A” to an integer.

# Consider the hexadecimal  string
my_str="0x12A"

print(my_str)

# Display the datatype
print("Actual Datatype: ",type(my_str))

# Convert to integer with base 16
converted=int(my_str,base=16)

# Integer with base 16
print("Integer with base 16: ",converted)

# Display the datatype
print("Modified Datatype: ",type(converted))

Output:

0x12A
Actual Datatype:  <class 'str'>
Integer with base 16:  298
Modified Datatype:  <class 'int'>

Previously it belongs to the str class. After converting to hexadecimal with base 16, the class is int.

Example 2:

In this example, we will convert the hexadecimal string “0x34D” to an integer.

# Consider the hexadecimal  string
my_str="0x34D"

print(my_str)

# Display the datatype
print("Actual Datatype: ",type(my_str))

# Convert to integer with base 16
converted=int(my_str,base=16)

# Integer with base 16
print("Integer with base 16: ",converted)

# Display the datatype
print("Modified Datatype: ",type(converted))

Output:

0x34D
Actual Datatype:  <class 'str'>

Integer with base 16:  845
Modified Datatype:  <class 'int'>

Previously it belongs to the str class. After converting it to hexadecimal with base 16, the class is int.

Summary

In this article, we have seen different ways to convert a string to an integer in python. First we started with using the int() function. Later we discussed converting binary and hexadecimal strings to an integer with base=2 and base=16. Happy Learning

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top