Python Convert String to Integer | Integer To String Tutorial

Last updated on: by Digamber
In this tutorial, we are going to learn how to convert Python string into Integer and Integer to String. Python offers lots of built-in functions to deal with a different situation. We have mentioned some of the very useful Python built-in functions below.

Throughout this Python type conversion tutorial we will be using some of the mentioned below Python built-in methods.Python type conversion mechanism is very powerful and beneficial in everyday programming. Python’s built-in functions made type conversion super easy when it comes to converting one data type into some other data type.

Useful Python Buil-in Type Conversion Functions

We’ve mentioned some extremely useful Python built-in function here, to help you in your day to day programming tasks. If you want to know more about Python, please visit Python official website and check their documentation.

int(value, base)This function converts the number and a string into an integer number. A base is the 2nd parameter in int() function and it referred to the number format.
float(value)The function converts the passed value into a floating point number.
ord(character)This function takes string or any character and return the unicode of a specific character.
hex(number)This function transforms the mentioned number into a hexadecimal value, and it includes 0x as a prefix with the returned value.
oct(int)This function is to convert integer to octal string.
This function transforms an integer into an octal string, and it includes 0o as a prefix with the returned value.
tuple(iterable)This function generates a tuple object, and it takes an iterator, sequence and collection object as an argument.
set(iterable)This function creates a set object, and It returns items in a list in an unordered and random format.
list(iterable)This function transforms any data type into a list object.
dict(keyword, arguments)This function creates a dictionary which is unordered, changeable and indexed.
str(object, encoding=encoding, errors=errors)This function converts the passed value into a string

Convert Python String to Integer Using int()

Use int() Python built-in function to convert a string into an integer. This method takes a string as a parameter and returns an integer. Now we are going to use int() Python function to convert string to int, check out the example below.

x = '30'
y = 15
z = int(x) + y
print ("The value of z is = ", z)
# Output: The value of z is =  45

The converted output of x + y value will be 45.

Convert Int to String in Python Using str()

The str() function can be used to convert int to string in Python. This function takes an object which could be int, float, double etc. Check out the given below example.

a = "Lorem ipsum"
num = 1234
print (a, str(num))

Following will be the output for the given above Python programme.

# Lorem ipsum 1234

Convert Python String to Integer From Other Base

Let us say if you want to convert a string into an integer, and it belongs to some other base than 10. In this case, you have to declare the base for the type conversion. However, you should remember 2 things: the 1st thing is that the output integer will be 10 and given base should be between 2 to 32. Check out the example below.

myint = '324'
convertInt = int(myint)
convertInt8 = int(myint, base=8)
convertInt16 = int(myint, base=16)
convertInt32 = int(myint, base=32)

Following will be the output:

# <class 'str'>
# <class 'int'>
# 324
# <class 'int'>
# 212
# <class 'int'>
# 804
# <class 'int'>
# 3140

Now, you might get ValueError when converting a string into an integer. This error usually occurs when your string value does not belong to numbers family. Let’s say if you did not pass the base-16 in int() method and still you are converting the hexadecimal numerical value to an integer. Then remember it will throw the ValueError exception.

Finally, we are done with Python type conversion tutorial, If you liked this tutorial then don’t forget to share this tutorial with others, have a good day to you.


I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.