Objects can be any kind of data type in Python, i.e. integer and string. Sometimes, you will need to convert one data form to another while writing Python code. For example, it needs to be transformed into an integer to perform a math operation on a number represented as a string. Here’s how to convert a string in Python to an integer.

int() function in Python

The built-in function int() returns an integer decimal object from a given number or String. It takes the form below:
int(a, base=10)

Two arguments are accepted by the function:

  • a – The number or String to be transformed to an integer.
  • base – Reflects the first argument’s numeral scheme. It can have a value between 0 and 2–36. It is optional to render this statement. The default base is always 10 if no base is defined (decimal integer). Integers are usually expressed in hexadecimal format i.e., base 16, decimal has base 10, octal has base 8, and binary has base 2.

If the given string cannot be interpreted as an integer, the ValueError exception is executed by the function.

Converting a String to Integer in Python

A ‘string’ is a list of characters declared in Python using a single (‘), double (“), or triple quotes (“””). If you use quotes to declare a variable that contains only numbers, its data type is set to String. Consider the instance below:

no_of_day = "23"
type(no_of_day)

The type() function tells us that the String object is the no_of_day variable.

<type 'str' > type

Let’s try on the variable to do a math operation:

print(day+5)

After that, Python throws an exception, i.e., TypeError, because it is unable to perform a string and integer addition calculation:

Traceback (last call last):
'' file, line 1, in
TypeError: can't concatenate items with 'str' and 'int.'

To convert a string representation of a decimal integer to an int, transfer a string that returns a decimal integer: to the int() function.

no_of_day = "23" days
no_of_day_int = int(no_of_day)
type(no_of_day_int)
<type 'int' >

The total operation will be done successfully if you try to do the math now:

print(no_of_day_int+5)
28

Remember, the commas you need to delete before passing the number to the int() function if the number contains commas, marking off thousands, millions, etc.:

total = "1,000,000"int(total.replace(",", ""))
1000000

When you execute the conversion between string to an integer, just make sure that you are using a correct base representing integers in various number systems. The number 54731 is expressed as D5CE in the hexadecimal method, for instance. You need to use Base 16 to convert it to a decimal integer:
If you move the D5CE String without setting the base to the int() function, the ValueError exception will be:

int ("D5CE", 16)
54731
Traceback (last call last):
'' file, line 1, in
ValueError: invalid literal with base 10 for int(): 'D5CF'

Conclusion:
In Python, using the int() function, you can convert a string to an integer.

Read:

Leave a Reply

Your email address will not be published. Required fields are marked *