How to convert string to int and float in python

how to convert string to in in python

In this article, you’ll learn how to convert string to integer and float in python. This is really very simple to do, the only different kind of situation occurs when the input string contains a comma.

1. How to convert string to int in python

We can convert string to integer in python using the in-built function int(). The following code snippets implements the same.

strInt = "53"
numInt = int(strInt)
print(f"The type of strInt is: {type(strInt)}")    #formatting using f-strings
print(f"The type of numInt is: {type(numInt)}")
sum = numInt + 10
print(f"{numInt} added to 10 is: {sum}")   

Output of the above code is:

The type of str is: <class 'str'>
The type of numInt is: <class 'int'>
53 added to 10 is: 63

If you don’t understand the f-strings that I’ve used in the code above, learn it in a minute from the article on strings in python.

2. How to convert string with comma to int in python

This is the situation what we were discussing about earlier. In python, string with comma can be converted to int very easily just by replacing the comma or space maybe, with a blank string.

The following code snippet illustrates how a string with comma can be converted to int in python.

strInt = "53,274"
numInt = int(strInt.replace(',',''))
print(f"Input type is: {type(strInt)}")    #formatting using f-strings
print(f"Converted data type is: {type(numInt)}")
sum = numInt + 100
print(f"100 added to {numInt} is: {sum}")

The output of the above code is:

Input type is: <class 'str'>
Converted data type is: <class 'int'>
100 added to 53274 is: 53374

As you can clearly see, the input string consisted of a comma, but still it got converted to an integer without a comma and error.

3. How to convert string to float in python

In python, we also have a built-in method for converting a string to float, i.e., float(). One can convert a number in string form to it’s float equivalent. Similar thing can be done using Decimal() function, however that will convert it to Decimal.decimal, but that can also be used a number for calculations.

Let’s have a look at the python program that implements the method to convert string to float in python.

strFloat = "3909.25"
numFloat = float(strFloat)
print(f"The type of strFloat is: {type(strFloat)}")    #formatting using f-strings
print(f"The type of numFloat is: {type(numFloat)}")
sum = numFloat + 30.20
print(f"{numFloat} added to 30.20 is: {sum}")

The output of the above code snippet is:

The type of strFloat is: <class 'str'>
The type of numFloat is: <class 'float'>
3909.25 added to 30.20 is: 3939.45

4. How to convert string with a comma to float in python

This is also a different situation where you might get an error like: ValueError: could not convert string to float , which means, you cannot convert string to float. This is because the input string might have something like a comma or a space, that might not be allowing you to convert string to a float.

ValueError: could not convert string to float
Value error due to wrong string data

A string with a comma can be converted to float using the inbuilt string method replace() to replace all the error causing characters from the string.

Let’s have a look at the following program that illustrates how to convert a string with comma to float in python.

strFloat = "42,937.43"
numFloat = float(strFloat.replace(',', ''))
print(f"The data type of strFloat is: {type(strFloat)}")    #formatting using f-strings
print(f"The data type of numFloat is: {type(numFloat)}")
sum = numFloat + 2000.040
print(f"{numFloat} added to 2000.040 is: {sum:.2f}")

Learn more about string formatting from the article on string in python.

Output of the above code snippet is:

The data type of strFloat is: <class 'str'>
The data type of numFloat is: <class 'float'>
42937.43 added to 2000.040 is: 44937.47

5. How to convert string to double in python

Converting a string to double is similar to converting string to float. Refer to above mentioned portion of the article to understand how to convert string to double in python.

Summary

In this article, we learned how to convert string to int, float and double. We also learned the special case where the string contains a comma that results into an error and we had to convert it to a int or float.

If this post helped you, share this with your programmer friends or on social media.

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →

Leave a Reply

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