Python String to Int() and Int to String

Python String to Int() and Int to String

To convert string to int and int to string python; In this tutorial, you will learn how to convert string to int and int to string python.

There are two python built-in methods, which are commonly used to convert string to int and int to string python; as shwon below:

  • Python int() is a built-in function, which is used to convert the string to an integer value.
  • Python str() is a built-in function, which is used to convert integer(int) to string.

How To Convert String to Int, Float and Int to String in Python

  1. Convert string to int in python.
  2. Convert str to int different base in Python.
  3. Convert str to int with commas in Python.

Python string to int

Python defines type conversion functions to directly convert one data type to another, which is useful in day to day and competitive programming.

Here, To get the data type of any variable in python using the type() function.

See the following example:

string = '123'
print(type(string))

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

num = 1234
print(type(num))

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

Different types of variables in above program. See the following output:

<type 'str'>
<type 'str'>
<type 'int'>
<type 'dict'>

As the above described about str() and int() built-in python. You can see below how to use str() and int() to converting a string to int and int to string. See the following example:

str = '455'
print(type(str))

convertedVal = int(str)
print(type(convertedVal))
print(convertedVal)

See the below output:

<type 'str'>
<type 'int'>
455

Just above the python program, we have converted the String to Integer in Python.

In this tutorial, you’ll learn how you can convert a Python string to an int with the above python program. Now, You’ll also learn how to convert an int to a string.

python program string to int:

str = "11"
print(eleven)

# Converting string to number
str1= int(str)
print(str1)

See the output:

11
11

Python str to int from different base

Strings can be transformed into numbers by using the int() and float() methods. If your string does not have decimal places, you will most probably want to convert it to an integer by utilizing the int() method.

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.

Let’s  see the following example:

str = '11'
print(type(str))

conInt = int(str)
print(type(conInt))
print(conInt)

conInt8 = int(str, base=8)
print(type(conInt))
print(conInt)

conInt16 = int(str, base=16)
print(type(conInt16))
print(conInt16)

conInt32 = int(str, base=32)
print(type(conInt32))
print(conInt32)

See the below output:

<type 'str'>
<type 'int'>
11
<type 'int'>
11
<type 'int'>
17
<type 'int'>
33

While converting from python string to int, you may get ValueError exception. The ValueError exception occurs if the string you want to convert does not represent any numbers. 

How to convert Python int to string example

Python str() is an inbuilt function that converts an integer to String.

See the following example.

hexValue = 0x2f22

print(hexValue)
print(type(hexValue))

hexValue = str(hexValue)

print(hexValue)
print(type(hexValue))

See the output below:

12066
<type 'int'>
12066
<type 'str'>

See another example of Int to String conversion in Python.

dg = 5024

print(dg)
print(type(dg))

dg = str(dg)

print(dg)
print(type(dg))

See the output following below:

5024
<type 'int'>
5024
<type 'str'>

Python decimal string to float number conversion

Now , you willl learn how to convert string to floating point number.

See the following example for a demonstration.

str = '11.21'
num = 19.21

el = float(str) + num
print ("The value of el = ",el)

See the following output.

('The value of el = ', 30.42)

Convert string numbers in a Python list to integers

If you intend to convert the string numbers contained in the python list, then one of the ways to convert those strings into an int is using a list comprehension.

A new list will be created where you may use the int in each iteration, as shown in the example below.

str_list = ['11', '19', '21']
int_list = [int(a) for a in str_list]
print (int_list)

See the following output:

➜  pyt python3 app.py
[11, 19, 21]
➜  pyt

Python str to int with commas

What about string numbers with commas like “11,000,000”. If you try to convert this string by using an int or float, it will generate the error because of the commas.

The solution to the above error is by importing the locale.

import locale

Now, you can use the USA locale setting like the following.

locale.setlocale( locale.LC_ALL, "en_US.UTF-8")

However, this may cause problems with different locations.

So, one other solution is to replace the comma by nothing. For example, see the code below.

# app.py

elevenmillion = '11,000,000'
eleven = int(elevenmillion.replace(',',''))

print ("The integer value", eleven)

See the following output.

➜  pyt python3 app.py
The integer value 11000000
➜  pyt

Python Built-in Functions:

Strings can be converted to numbers using the int() and float() functions. So this is how you can convert Python string to a number.

If the string does not have decimal places, you’ll most likely want to convert it to an integer by using the int() method.

  • The str() function is used to convert the integer to String in Python.
  • The ord() function is used to convert the character to an integer.
  • The hex() function is used to convert an integer to a hexadecimal string.
  • The oct() function is to convert an integer to the octal string.

Recommended Python Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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