Python string to int and int to string

Python String To Int, Int To String

In this article, we will understand the conversion of Python String to Int and int to string conversion. In Python, the values are never implicitly type-casted. Let’s find out how to explicitly typecast variables.

1. Python String to int Conversion

Python int() method enables us to convert any value of the String type to an integer value.

Syntax:

int(string_variable)

Example:

string_num = '75846'
print("The data type of the input variable is:\n")
print(type(string_num))

result = int(string_num)
print("The data type of the input value after conversion:\n")
print(type(result))
print("The converted variable from string to int:\n")
print(result)

Output:

The data type of the input variable is:
<class 'str'>
The data type of the input value after conversion:
<class 'int'>
The converted variable from string to int:
75846

Conversion of Python String to int with a different base

Python also provides us with an efficient option of converting the numbers/values of the String type to integer values under a particular base in accordance with the number system.

Syntax:

int(string_value, base = val)

Example:

string_num = '100'
print("The data type of the input variable is:\n")
print(type(string_num))

print("Considering the input string number of base 8....")
result = int(string_num, base = 8)
print("The data type of the input value after conversion:\n")
print(type(result))
print("The converted variable from string(base 8) to int:\n")
print(result) 

print("Considering the input string number of base 16....")
result = int(string_num, base = 16)
print("The data type of the input value after conversion:\n")
print(type(result))
print("The converted variable from string(base 16) to int:\n")
print(result) 

In the above snippet of code, we have converted ‘100’ to the integer value with base 8 and base 16 respectively.

Output:

The data type of the input variable is:

<class 'str'>
Considering the input string number of base 8....
The data type of the input value after conversion:

<class 'int'>
The converted variable from string(base 8) to int:

64
Considering the input string number of base 16....
The data type of the input value after conversion:

<class 'int'>
The converted variable from string(base 16) to int:

256

ValueError Exception while Python String to int conversion

Scenario: If any of the input string contains a digit that does not belong to the decimal number system.

In the below example, if you wish to convert string ‘A’ to an integer value of A with base 16 and we do not pass base=16 as an argument to the int() method, then it will raise ValueError Exception.

Because even though ‘A‘ is a hexadecimal value, still as it does not belong to the decimal number system, it won’t consider A to be equivalent to decimal 10 unless and until we don’t pass base = 16 as an argument to the int() function.

Example:

string_num = 'A'
print("The data type of the input variable is:\n")
print(type(string_num))
result = int(string_num)

print(type(result))
print("The converted variable from string(base 16) to int:\n")
print(result) 

Output:

The data type of the input variable is:

<class 'str'>
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    result = int(string_num)
ValueError: invalid literal for int() with base 10: 'A'

Converting a Python list of Integer to a list of String

Python list containing integer elements can be converted to a list of String values using int() method along with List Comprehension.

Example:

st_lst = ['121', '144', '111', '564']

res_lst = [int(x) for x in st_lst]
print (res_lst)

Output:

[121, 144, 111, 564]

2. Python int to String Conversion

Python str() method enables us to convert any value of the integer type to an String value.

Syntax:

str(integer_value)

Example:

int_num = 100
print("The data type of the input variable is:\n")
print(type(int_num))
result = str(int_num)
print("The data type of the input value after conversion:\n")
print(type(result))
print("The converted variable from int to string:\n")
print(result) 

Output:

The data type of the input variable is:
<class 'int'>
The data type of the input value after conversion:
<class 'str'>
The converted variable from int to string:
100

Conclusion

In this article, we have understood the conversion of Python String to Integer and vice-versa.


References