How do you convert string to integer in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In Python an strings can be converted into a integer using the built-in int[] function. The int[] function takes in any python data type and converts it into a integer.But use of the int[] function is not the only way to do so. This type of conversion can also be done using thefloat[] keyword, as a float value can be used to compute with integers.

    Below is the list of possible ways to convert an integer to string in python:

    1. Using int[] function

    Syntax: int[string]

    Example:

    num = '10'

    print[type[num]] 

    converted_num = int[num]

    print[type[converted_num]]

    print[converted_num + 20]

    As a side note, to convert to float, we can use float[] in Python

    num = '10.5'

    print[type[num]] 

    converted_num = float[num]

    print[type[converted_num]]

    print[converted_num + 20.5]

    2. Using float[] function

    We first convert to float, then convert float to integer. Obviously the above method is better [directly convert to integer]

    Syntax: float[string]


    Example:

    a = '2'

    b = '3'

    print[type[a]]

    print[type[b]]

    a = float[a]

    b = int[b]

    sum = a + b

    print[sum]

    Output:

    class 'str'
    class 'str'
    5.0

    Note: float values are decimal values that can be used with integers for computation.


    When you're programming, you'll often need to switch between data types.

    The ability to convert one data type to another gives you great flexibility when working with information.

    There are different built-in ways to convert, or cast, types in the Python programming language.

    In this article, you'll learn how to convert a string to an integer.

    Let's get started!

    Data types in Python

    Python supports a variety of data types.

    Data types are used for specifying, representing, and categorizing the different kinds of data that exist and are used in computer programs.

    Also, different operations are available with different types of data – one operation available in one data type is often not available in another.

    One example of a data type is strings.

    Strings are sequences of characters that are used for conveying textual information.

    They are enclosed in single or double quotation marks, like so:

    fave_phrase = "Hello world!"
    
    #Hello world! is a string,enclosed in double quotation marks
    

    Ints, or integers, are whole numbers.

    They are used to represent numerical data, and you can do any mathematical operation [such as addition, subtraction, multiplication, and division] when working with integers.

    Integers are not enclosed in single or double quotation marks.

    fave_number = 7
    
    #7 is an int
    #"7" would not be an int but a string, despite it being a number. 
    #This is because of the quotation marks surrounding it
    

    Data type conversion

    Sometimes when you're storing data, or when you receive input from a user in one type, you'll need to manipulate and perform different kinds of operations on that data.

    Since each data type can be manipulated in different ways, this often means that you'll need to convert it.

    Converting one data type to another is also called type casting or type conversion. Many languages offer built-in cast operators to do just that – to explicitly convert one type to another.

    To convert, or cast, a string to an integer in Python, you use the int[] built-in function.

    The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed.

    The general syntax looks something like this: int["str"].

    Let's take the following example, where there is the string version of a number:

    #string version of the number 7
    print["7"]
    
    #check the data type with type[] method
    print[type["7"]]
    
    #output
    
    #7
    #
    

    To convert the string version of the number to the integer equivalent, you use the int[] function, like so:

    #convert string to int data type
    print[int["7"]]
    
    #check the data type with type[] method
    print[type[int["7"]]]
    
    #output
    
    #7
    #
    

    A practical example of converting a string to an int

    Say you want to calculate the age of a user. You do this by receiving input from them. That input will always be in string format.

    So, even if they type in a number, that number will be of .

    If you want to then perform mathematical operations on that input, such as subtracting that input from another number, you will get an error because you can't carry out mathematical operations on strings.

    Check out the example below to see this in action:

    current_year = 2021
    
    #ask user to input their year of birth
    user_birth_year_input = input["What year were you born? "]
    
    #subtract the year the user filled in from the current year 
    user_age = current_year - user_birth_year_input
    
    print[user_age]
    
    #output
    
    #What year were you born? 1993
    #Traceback [most recent call last]:
    #  File "demo.py", line 9, in 
    #    user_age = current_year - user_birth_year_input
    #TypeError: unsupported operand type[s] for -: 'int' and 'str'
    

    The error mentions that subtraction can't be performed between an int and a string.

    You can check the data type of the input by using the type[] method:

    current_year = 2021
    
    #ask user to input their year of birth
    user_birth_year_input = input["What year were you born? "]
    
    print[type[user_birth_year_input]]
    
    #output
    
    #What year were you born? 1993
    #
    

    The way around this and to avoid errors is to convert the user input to an integer and store it inside a new variable:

    current_year = 2021
    
    #ask user to input their year of birth
    user_birth_year_input = input["What year were you born? "]
    
    #convert the raw user input to an int using the int[] function and store in new variable
    user_birth_year = int[user_birth_year_input]
    
    #subtract the converted user input from the current year
    user_age = current_year - user_birth_year
    
    print[user_age]
    
    #output
    
    #What year were you born? 1993
    #28
    

    Conclusion

    And there you have it - you now know how to convert strings to integers in Python!

    If you want to learn more about the Python programming language, freeCodeCamp has a Python Certification to get you started.

    You'll start with the fundamentals and progress to more advance topics like data structures and relational databases. In the end, you'll build five projects to practice what you learned.

    Thanks for reading and happy coding!

    Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

    How do you turn a string into an integer?

    parseInt[] to convert a string to an integer..
    Use Integer. parseInt[] to Convert a String to an Integer. This method returns the string as a primitive type int. ... .
    Use Integer. valueOf[] to Convert a String to an Integer. This method returns the string as an integer object..

    What does str [] do in Python?

    The str[] function converts values to a string form so they can be combined with other strings.

    How do you convert a string to a variable in Python?

    Instead of using the locals[] and the globals[] function to convert a string to a variable name in python, we can also use the vars[] function. The vars[] function, when executed in the global scope, behaves just like the globals[] function.

    How do you convert to number in Python?

    Number Type Conversion Type int[x] to convert x to a plain integer. Type long[x] to convert x to a long integer. Type float[x] to convert x to a floating-point number. Type complex[x] to convert x to a complex number with real part x and imaginary part zero.

    Chủ Đề