Home Python How to Convert a Python String to Integers

How to Convert a Python String to Integers

How to Convert a Python String to Integers

Wondering to convert a Python string to integers?

When you’re modifying in Python, it will put the information you are working away in various manners.

In case you’re working with text, your report will be put away as a string.

In any case, in case you’re working with a decimal number, your information will be organized as a buoy.

This is significant because each sort of information can be controlled unexpectedly.

Subsequently, Python needs to store various kinds of information utilizing distinctive information types.


Python String to Int

There are different information types in Python used to store data, including numbers, Booleans, and records.

In this article, we will zero in on two of these information types: strings and numbers.

In Python, strings are encased inside single or twofold statements and are used to address text-based data. Here’s an illustration of a string:

text_data = "This string contains text-based information".

The number information type is used to store both decimal and absolute numbers.

Will naturally be changed Decimal numbers over into a buoy, and absolute numbers will be viewed as a whole number.

Here’s an illustration of a number and a drifting point number in Python:

Example_integer = 22

Example_float = 2.22

Changing from an information type to another type of information is called type transformation.

In case you need to change a string over to a number or a whole number to a string, you will play out a kind transformation activity.

Python can convert strings over into a whole number utilizing the inherent int() work.

Int () Python

The int() work takes in any python information type and converts it into an integer. But utilization of the int() work isn’t the best way to do as such.

This kind of transformation should likewise be possible using the float() keyword, which can use as a buoy worth to figure with numbers.

The int() technique can change a string over to a whole number worth in Python.

Int () takes in two boundaries: the string to change over into a whole number and the base you need your information to address. The subsequent boundary is discretionary.

The technique returns the worth you went through int(), addressed as a number.

Here’s the language structure for the Python int() strategy:

int(number, base=base)

Here’s an illustration of the int() technique being used to change a string over to a number:


Our code returns:

  1. 12

An Example of String to Int in real life

How about we utilize a more point-by-point guide to show how the int() technique can be used.

Say that we are making a sign-up structure for youngsters who request the client’s age.

We need this worth to be put away as a number in our data set. Yet, when a client embeds the value in our program, their age turns into a string.

How about we make a program that plays out this capacity.

We’ll begin by using the information() strategy to get a client’s age:

raw_user_age = input("What is your age?")

At the point when our client enters a number, we will print it out to the reassure. When we continue our program, this will happens as a result:

What is your age?



The worth the client embedded is 12. This may resemble a number.

Use the sort() strategy

When we use the sort() strategy to check the information type for our client’s age, we see nothing but a number.

We can utilize the accompanying code to check the information kind of our client’s age:


Our code returns:

  1. class ‘str’

As should be obvious, our information is put away as str or a string. In this way, we need to change our data over to a number. We can do such by using the accompanying code:

raw_user_age = input("What is your age?")
user_age = int(raw_user_age)

This is what happens when we execute our code and supplement the worth 12:

  1. What is your age?
  2. 12
  3. 12

Our code returns a similar yield as above. However, 12 is currently put away as a number. We can utilize the sort() technique to check:


Our code returns:

  1. class ‘int’

ValueError Exception while Python String to int transformation

Situation: If any of the info strings contains a digit that doesn’t have a place with the decimal number framework.

In the underneath model, in case you wish to change over string ‘A’ to a number worth of A with base 16 and we don’t pass base = 16 as a contention to the int() technique, at that point, it will raise ValueError Exception.

Since even though ‘A’ will be hexadecimal worth, still as it doesn’t have a place with the decimal number framework, it will not believe A to be identical to decimal ten except if and until we don’t pass base = 16 as a contention to the int() work.


string_num = 'A'

print("The information sort of the info variable is:\n")


result = int(string_num)


print("The changed over factor from string(base 16) to int:\n")



The information sort of the info variable is:

class ‘str’

Traceback (latest call last):

Document “main.py”, line 4, in module

result = int(string_num)

ValueError: invalid exacting for int() with base 10: 'A'

Read more: How to Convert Float to an Int In Python


Please enter your comment!
Please enter your name here