Python string to int conversion tutorial with examples

A brief guide on Python string to int conversion with few examples. It has some built-in function for converting an int to string and string to integers.

We all know that python provides us a lot of built-in functionality and methods that can ease the life of the programmer.

Before jumping on to the conversion, let’s understand the data types first. This will help us understand how int and string behave in python.

Integers in Python

Let’s talk about integers first. An integer is a data type used to store the integers values. We can also call them an immutable data type.

This means that if you change the value of data type then the result will be stored in the newly created object.

For Example :

a = 5
b = 7
print(‘The value of a is : ’, a)
print(‘The value of b is : ’, b)
print(‘The data type of a is : ’ , type(a))
print(‘The data type of b is : ’ , type(b))

Output :

The value of a is : 5
The value of b is : 7
The data type of a is :<class ‘int’>
The data type of b is : <class ‘int’>

Explanation : 

In the above code, a and b are the integer variable that is holding the integer value. The type() function in python is used to get or find the data type of the given variable.

[su_label type=”success”]Suggested read[/su_label] Understand python loops with examples

Strings in Python

The string is a very important data type in any programming language. In python, the string data type is represented by str.

We all know that string is a sequence of characters. All the characters inside double quotes  “ ” or inside single ’ ’ quotes is called a string in python.

For Example :

a = ‘Python is very advance programming language’
print(a)
print(type(a))

Output :

Python is very advance programming language
<class ‘str’ >

Explanation :

Here a is a string data type that is holding a string value. The type() is used to get the data type of the variable.

Now let’s talk about converting a string into an integer. The int() function in python is used to convert the string into the integer value. And if you want to convert an integer value into a string then we use the string() method. The string() method is responsible for converting the integer value into the string and int() function is responsible for converting string value into an integer.

[su_label type=”success”]Suggested read[/su_label] How to compare two strings in python

The int() takes an argument as a string and convert it into an integer value. The string() function takes an integer argument and converts it into the string.

Let’s try to understand it with an example.

Converting a string to int

Example code: 

s = ‘6321’
print(‘ The data type of s is : ’ , type(s))
i = int(s)
print(‘The data type of i is :  ‘ , type(i))
print(‘ the converted value is : , i)

Output: 

The data type of s is :   < class ‘str’ >
The data type of i is :    < class ‘int’ >
the converted value is : 6321

[su_label type=”success”]Suggested read[/su_label] How to write to file in python

Converting a string to int with a different base

Strings can be changed into numbers by utilizing the int() and float() techniques.

In the event that your String doesn’t have decimal points, you will most likely need to change over it to a whole number by using the int() strategy.

On the off chance that the String you need to change over into int has a place with a different number base other than base 10, at that point you can determine that base for that transformation.

Example code :

s = ‘123’
print(‘the data type of s is : ‘ , type(s))

i = int(s)
print(‘the value of i is : ‘, i)
print(‘the data type of i is : ‘ , type(i))

i_8 = int(s,base = 8)
print(‘the data type of i_8 is : ‘ , type(i_8))
print(‘the value of i_8 is : ‘, i_8)

i_16 = int(s, base = 16)
print(‘the data type of i_16 is : ‘ , type(i_16))
print(‘the va;ue of i_16 is : , i_16)

i_32 = int(s,base = 32)
print(‘the data type of i_32 is : ‘ , type(i_32))
print(‘the value of i_32 is : ‘ , i_32)

The output of the code : 

The data type of s is :  <class ‘str’ >
The value of i is : 123
The data type of i is :   <class ‘int’ >
The data type of i_8 is :   <class ‘int’ >
The value of i_8 is : 123
The data type of i_16 is:   <class ‘int’ >
The value of i is : 291
The data type of i_32 is:   <class ‘int’ >
The value of i is : 1091

Conclusion :

There is a lot of functionality in python that can ease your hard work and increase your productivity towards your main project. Python string to int conversion is one of them.

Leave a Comment