HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

7) Numbers and Math Lesson

Python String to Int

7 min to complete · By Martin Breuss

In Python, the data type of a variable can change over its lifetime, and converting from string to integer is a common application. You can often choose to convert the data type of a variable into another data type. There are two ways that this conversion can happen:

  1. Implicit type conversion
  2. Explicit type conversion

In the previous lesson, you encountered an example of implicit type conversion. Wait... What exactly happened there again?

Implicit Type Conversion

When you perform a calculation with two integers, but the result can't be accurately represented as an integer, then Python automatically changes the data type of the output to a float:

whole = 1
part_of_whole = whole / 2
print(part_of_whole)  # 0.5

This is called implicit type conversion, because you didn't have to tell Python to change the type. Python did so implicitly.

Implicit type conversion can be very helpful and is generally designed in a way so that you don’t need to think about it too much. Python applies probably useful rules and does all the work behind the scenes.

Implicit Type Conversion Examples

  • Divisions that produce results with a remainder
  • Calculations that involve an int and a float
  • Insertions of variables into f-strings
Colorful illustration of a light bulb

Info: You haven't encountered f-strings yet, but you will learn more about them in the upcoming section.

When you use f-strings, Python evaluates expressions and silently converts everything to the str type. The output is always of type str! Therefore, anything you dump in there gets implicitly converted to a str. You'll learn more about f-strings and the str data type soon.

Explicit Type Conversion

In some cases, it's possible to tell Python to convert a value to a different data type. For example, it's possible to convert text that is a number into an actual number. This is commonly necessary when you're receiving user input in a Python script, which always first arrives as text of the data type str.

Convert String to Int in Python

Look at the explicit type conversion example below. Again, don't worry if you don't exactly understand what's going on here, and remember to try to read it as English pseudocode. Then, see whether you can somewhat understand what the code is about:

userAge = "33"
userAge = int(userAge)  # int stands for 'integer'
print(userAge, type(userAge))

The code above explicitly changes the type of the variable userAge from a str to an int. Input from a user that is gathered with input() always arrives as a text value. Therefore, in order to be able to perform a number-based calculation with it, you'll first need to explicitly convert it to a number. Give it a go in the playground below:

Code Playground

userAge = "33"
print(type(userAge))  # Checking current type

# Convert 'userAge' to an integer below
# to make the calculation and avoid the TypeError

# ENTER YOUR CONVERSION CODE HERE

print("Next year the user will be: ", userAge + 1)

Collecting user input and then wanting to continue working with it is a common situation where you may need to apply explicit type conversion in Python.

Colorful illustration of a light bulb

Info: If you think back to your guess-the-number game at the beginning of this course, then you might remember that you also needed to convert the input provided into a number. Otherwise, Python couldn't compare it to the randomly generated number that you had to guess.

Can you think of a script you would like to build that collects a number input from a user and makes a calculation with it? Feel free to use the playground above to build a draft and share it on the Discord forums.

Colorful illustration of a light bulb

Additional Resources

Summary: Python String to Int

  • Convert a Python str to int with explicit conversion
  • int(X) is a Python method that will convert X to an int
  • Calculations that involve an int and a float, or produce a remainder are examples of implicit conversion
  • Insertions of variables into f-strings is another common implicit conversion example

Kinds of Type Conversion in Python

  • Implicit type conversion: Python automatically converts the type of a value.
  • Explicit type conversion: You tell Python to convert the type of a value.