Skip to main content
python-str-to-int

How To Convert String to int and int to string

This tutorial will assist you in understanding Python type conversion. We will learn how to convert a Python String to an int and an int to a Python String.

Python defines type conversion functions that allow you to directly convert one data type to another.

Integers are whole numbers that can be stored in string or int type variables. Python includes built-in type conversion functions for converting one data type to another.

The integer value stored integer using a string literal:
num = "127"

The integer value stored integer using a integer data type:
num = 127

Python String to Int

I’m going to take a string variable and turn it into an integer. There is a condition in the application that requires the conversion of a string to an int. I’m reading data from a JSON file. Some of the data is in String format, and I’ll have to convert it to an int.

Python have int() method that converts a string into an int. The int() function with a string containing a number as an argument returns the number converted to an integer.

# string data

num = '127'

# convert string into int using int()

num = int(num)

# print the type again

print('Now, type of num is :', type(num))

We are using type() function to get the data type of any variable.

The above method throws a ValueError if the string you want to convert does not contain any numbers.

Python Int to String

In addition, we can convert integer data to string data. There is a condition in the application that requires the conversion of an integer to string data. The str() a method in Python converts a string to an int.

The str() method is used to convert int type data into the integer. This method takes a string containing the number as the argument, and it returns the number converted to an integer.

# string data

num = 127

# convert int into string using str()

num = str(num)

# print the type again

print('Now, type of num is :', type(num))

How To Convert float string to float number

The python has a float class that helps to convert a decimal string to a float number. Let’s have a look into the following example –

num = '127.41'
print ("The value of num = ", float(num))

How To Convert commas Seperated str to int

We have a variable that has comma-separated values and need to create sum, and validate sum condition. I have “127,128,124” values into a single string, I need to convert this string by using an integer, it generated the error when I have applied int() method. I have applied int() method into the comma-separated –

#import requests
import locale
import pprint

marks_str = '127,128,124'
marks = int(marks_str)

print ("The integer value of marks =", marks)

The error message –

C:\workflow_python>py string_int.py
Traceback (most recent call last):
  File "string_int.py", line 6, in 
    marks = int(marks_str)
ValueError: invalid literal for int() with base 10: '127,128,124'

We can convert comma separated string values into integers using locale packages. Just need to import the package into our python file –

import locale
import pprint
marks_str = '127,128,124'
marks = map(int, marks_str.split(","))
print ("The integer value of marks =", list(marks))

Result :

C:\workflow_python>py string_int.py
The integer value of marks = [127, 128, 124]

Leave a Reply

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