How to convert strings to integers in Python – with example

  • Reading time:16 mins read

How to convert strings to integers or floats without getting an error message?

In this article, we will learn how to convert strings to integers and backwards.

Now, this is something that you will probably come across quite often, especially when you’re working with external data such as user input or a dataset sourced from the web.

 

 

Suppose you’re working with a data set containing numbers that are actually stored in string format, in such a scenario if the intention is to perform some arithmetic operations on the data then we need to convert the data into integer type first.

 

How to convert strings to int in Python?

 

How to convert strings to integers – easiest way

In Python, integers are whole numbers with no decimal places and can be represented as textual data also known as strings or in numeric type as integers.

In order to show you, how to convert strings to integers, we will use the built-in function int() which converts a specified value into an integer object.

In the example below we have created a string called mystring that we have initialized with the value 100 in quotes in order to make it a string.

To ascertain that this is indeed a string we can use the type() method which returns the class type of the object passed as a parameter within the parentheses.

 

 

Now since this is a string that means that if we try to use the variable mystring as an integer in an arithmetic operation we are bound to get a Type Error

 

Parsing variables

To successfully perform the arithmetic operation we first need to convert the string mystring into an integer.  We can do that by wrapping the variable name mystring with the int() function as shown below.

 

 

We should also note that in order to successfully convert a string of such kind to an integer using the function int() it should be void of any decimal points.

 

How to convert strings to int in Python?

 

Converting an integer to a string – how to convert strings to integers and back

We can equally convert an integer to a string object using the built-in function str().

Suppose we have an integer that we wish to concatenate with a string, this could be a URL that we want to add a number in order to access a specific item.

Now let’s create an arbitrary integer called myint = 4567. We can also verify that this is an integer using the type() method.

 

 

Now when we try to concatenate the above integer with a string will also certainly get a Type Error with a slightly different message this time letting us know that we can only concatenate a string to a string.

 

 

Now using the str() function we can easily convert the integer myint to a string so that we can now concatenate the two as shown below.

 

 

Working with real data type conversion can be more complex than this, however, it is better when you recall the basic functions int() and str().

 

How to convert strings to int in Python?

 

Converting a float to a string

A  floating-point number is a number with decimal places. A floating-point number can also be represented as a string.

Using the float() function we can convert a string or an integer into a floating-point number. Using the str() function we can equally convert a float to a string. 

 

 

To convert the string above into float we will use the float function as shown below.

 

 

It is worth noting that although we have previously stated that we can convert strings to integers, we cannot convert strings that have a decimal point such as the one below to integers.

Any attempt to do so will result in a ValueError.

 

Summary

This is how to convert strings to integers or floats in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.