How to Convert Python String to int?

Posted in /   /  

How to Convert Python String to int?
vinaykhatri

Vinay Khatri
Last updated on April 20, 2024

    In Python, integer numbers are represented as the whole numbers with no fraction value. In Python using the int and str data types we can store integer values however the arithmetic operators do not work on str datatype.  In this tutorial, we have provided the python code snippets which can convert Python str datatype to an int datatype.

    Represent an Integer value in Python

    An integer value can be stored as str and int data type, but it always suggested to save it as int.

    Integer value as an str data type

    >>> a = "200"

    Integer value as an int data type

    >>> a = 200

    How to Convert Python String to int?

    In Python, we have a built-in function int() which can convert str datatype integer value to int datatype integer value. But while using the int() function we need to make sure that the string contains only integer or whole number, nothing else.

    Example

    s = "400"
    i = int(s)
    print(s)

    Output

    400

    <Note>: The string value which we want to convert into an integer value must contain only whole or integer number else the int() function return an error.

    Example

    s = "200k"
    i = int(s)
    
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: '200k'

    int() function parameters

    The int() function can accepts two parameters:

    • value.
    • base.

    Syntax

    int(value , base)

    Here the base signifies the base value of the passed value, and by default, the base value is 10(decimal). There are 4 major number systems we can use to represent integer and whole numbers.

    • Binary (base 2)
    • octal (base 8)
    • decimal (base 10)
    • hexadecimal (base 16)

    How to Convert Python Binary to int

    When we want to convert a binary number to an integer value, then we need to pass its base value 2.

    Example

    >>>i = int(30.23)
    >>> b = "1000"
    >>> n = int(b, base=2)
    >>> n
    8

    How to Convert Octal to int in Python

    To convert an octal number to an integer, we need to mention the base 8. Octal numbers can only be represented using 0, 1, 2, 3, 4, 5, 6, and 7 numbers. 10 in octal means 8 in decimal or integer.

    Example

    >>> o = "10"
    >>> i = int(o, base=8 )
    >>> i
    8

    Decimal numbers

    By default the int() function change all the valid values to decimal values.

    Example

    >>> d = "129"
    >>> n = int(d, base= 10)
    >>> n
    129

    Convert Hexadecimal to int() in Python

    To change a hexadecimal number to an integer, we need to pass the base value as 16. Hexadecimal can represent a number using 0 to 9 and A-F, which make its base count to 16.

    Example

    >>> hd = "1F"
    >>> n = int(hd, base= 16)
    >>> n
    31

    Convert int to str in Python

    Similar to the int() function we have str() function in Python which can convert the passed value to string datatype.

    Example

    >>> f = 30.23
    >>> s = str(f)
    >>> s
    '30.23'
    
    >>> type(s)
    <class 'str'>

    Note: T he type() function returns the data type of the variable or object.

    Summary

    • The inbuilt int() function can convert the passed string value to its corresponding integer value.
    • The string value we passed in the int() function must be an integer value or whole number, the function returns an error.
    • The base parameter of int() function represents the number system base of the value.
    • The str() function can convert any value to a string data value.

    People are also reading:

    Leave a Comment on this Post

    0 Comments