AppDividend
Latest Code Tutorials

How To Convert Python String to Int and Int to String

0 38

Get real time updates directly on you device, subscribe now.

In this tutorial, we will see How To Convert Python String to Int and Int to String. Just like the str() built-in function, Python also offers a handy built-in which takes a string object as an argument and returns an integer object. Python defines the type conversion functions to directly convert one data type to another data type which is useful in day to day competitive programming. The int() function is a Python standard built-in function to convert the string into an integer value. You call it with the string containing the number as the argument, and it returns the number converted to an integer.

How To Convert Python String to Int

We can use the type function to get the data type of any variable. See the following example.

# app.py

amp = '123'
print(type(amp))

eli = 'The Computer Guy'
print(type(eli))

num = 1234
print(type(num))

ser = [1, 2, 4]
print(type(ser))

dict = {1: 'Android', 2: 'iOS', 3: 'Symbian'}
print(type(dict))

We have taken the different types of variables and printed on the Python Console. See the following output.

How To Convert Python String to Int

If you want to convert the number that is represented in a string to int, you have to use the int() function to do so. See the following example.

# app.py

amp = '123'
print(type(amp))

convertedInt = int(amp)
print(type(convertedInt))
print(convertedInt)

See the below output.

How To Convert Python String to Int and Int to String

We have converted the String into an Integer.

Converting String to Int from different base

If the string you want to convert into int belongs to a different number base other than base 10, then you can specify that base for that conversion. But one thing, you need to keep in mind that the output integer is always in base 10. 

Another thing you need to remember is that a given base must be in between 2 to 32.  See the following example.

# app.py

amp = '123'
print(type(amp))

convertedInt = int(amp)
print(type(convertedInt))
print(convertedInt)

convertedInt8 = int(amp, base=8)
print(type(convertedInt))
print(convertedInt)

convertedInt16 = int(amp, base=16)
print(type(convertedInt16))
print(convertedInt16)

convertedInt32 = int(amp, base=32)
print(type(convertedInt32))
print(convertedInt32)

See the below output.

Converting String to int from different base

While converting from string to int, you may get ValueError exception. The ValueError exception occurs if the string you want to convert does not represent any numbers. If you want to convert the hexadecimal number to an integer. But you did not have to pass argument base=16 in the int() function. It will raise the ValueError exception if there is any digit that does not belong to the decimal number system.

Convert Python Int to String Tutorial

You can use the str() function to convert an integer to String in Python. See the below example.

# app.py

hexValue = 0x2f22
print(hexValue)
print(type(hexValue))
hexValue = str(hexValue)
print(hexValue)
print(type(hexValue))

See the output below.

Convert Python Int to String Tutorial

See another example of Int to String conversion in Python.

# app.py

digit = 1921
print(digit)
print(type(digit))
digit = str(digit)
print(digit)
print(type(digit))

Convert Python Int to String Tutorial Example

Finally, How To Convert Python String to Int and Int to String Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

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