• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Convert String to Integer with int() in Python

Convert String to Integer with int() in Python

August 13, 2022 Leave a Comment

To convert a string variable to an integer in Python, use the int() function.

x = "1"
y = int(x)

print(y)

#Output:
1

When working with string variables in Python, the ability to easily be able to use and change these variables is valuable.

One such situation is if you want to create an integer from a string variable.

To convert a string variable to an integer in Python, use the int() function.

int() takes a string variable which represents an integer, such as “12” or “4”, and returns an integer variable.

Below shows a few simple examples of how you can use int() to convert a string to an integer in Python.

a = "1"
b = "12"
c = "43"

print(int(a))
print(int(b))
print(int(c))

#Output:
1
12
43

Using int() to Convert String to Integer in Python

To use int(), there are a few things you should know. First, you need to pass a string literal representing an integer.

For example, if you try to pass a string which has letters, then you will get a ValueError.

You will also get a ValueError if you pass a string which representing a numeric value that is a floating-point number. If you want to convert a string to a float in Python, then use float().

Below shows a few more examples of how to use int() and shows some common errors which might come from the use of int().

a = "1"
b = "1.23"
c = "abc"

print(int(a))
print(int(b))
print(int(c))

#Output:
1
ValueError: invalid literal for int() with base 10: '1.23'
ValueError: invalid literal for int() with base 10: 'abc'

Hopefully this article has been useful for you to learn how to convert a string to integer with Python.

Other Articles You'll Also Like:

  • 1.  Changing Python Turtle Speed with speed() Function
  • 2.  Subtract Seconds from Datetime Variable Using Python timedelta() Function
  • 3.  Symmetric Difference of Two Sets in Python
  • 4.  Get pandas Index Values as List in Python
  • 5.  Append Multiple Elements to List Using Python
  • 6.  Check if Class is Subclass in Python with issubclass()
  • 7.  Using Python to Print Environment Variables
  • 8.  Remove Brackets from String Using Python
  • 9.  Draw Circle in Python Using turtle circle() Function
  • 10.  Python min() function – Get Minimum of List or Dictionary with min() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy