Python Int to string Tutorial

Posted in /  

Python Int to string Tutorial
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    To convert a Python integer value to a string, we can use Python's inbuilt str() function. The str() function accepts a single argument and returns the string value of that argument by converting that argument value into a string. The syntax for converting an integer value to a string is as follows str(Int_Value) .

    Python supports many data types, so we can easily distinguish one data type value from another.  For instance, we have int datatype to represent integer values, float datatype to represent real numbers , str datatype to represent textual characters and many other data types to store multiple values.

    How to Convert a Python Integer value to String?

    While writing a program , we encounter cases where we need to convert the data type of one object to another. In that case, we can use the different inbuilt functions provided by Python to convert the data type of the object.

    In this tutorial, we will explore the Python str() function, and learn how to use it to convert the integer value to a string. We will also discuss a common example where this conversion could save time and build simple logic.

    Python Int to String

    Python comes with an inbuilt function str(), which can accept an integer value as an argument and return it as a string value.

    syntax

    str(integer)

    return value

    The str() function does not convert the actual value. Instead, it returns a string copy of the value.

    Example

    # integer
    integer_num = 343
    
    # convert the integer to string
    str_num = str(integer_num)
    
    # type of integer_num
    print(f"The data type of integer_num({integer_num}) is:", type(integer_num))
    
    #type of str_num
    print(f"The data type of str_num({str_num}) is:", type(str_num))

    Output

    The data type of integer_num(343) is: <class 'int'>
    The data type of str_num(343) is: <class 'str'>

    Common Scenario Example

    There are many problems where you can use the str() function to convert an integer value to a string. And the two most common examples where you could use this data type conversion are:

    1. Analyze an integer value, such as finding the total number of digits.
    2. Convert a Python list of integers to a list of strings.

    1. Analyze an Integer value in Python

    Let say we have a Python integer value, and we need to find out the total number of digits present in that value. Although this problem can be solved by putting a smile logic with the help of a count variable and while loop. But if we convert the integer value to a string, this task can be performed using a single line of code statement.

    Example

    # given integer
    number = 1009379372
    
    # convert the integer number to string
    str_num = str(number)
    
    total_digits = len(str_num)
    
    print(f"The total number of digits in {number} is: {total_digits}")

    Output

    The total number of digits in 1009379372 is: 10

    Not only this, but by converting the integer value to a string, we can also perform many other tasks with ease and without implementing the logic.

    2. Convert a Python list of strings to a list of integers

    If we perform the str() function on a list object, it will convert the complete list into a string, not the individual elements.

    Example

    # list of integer values
    num_list = [10, 23, 45, 68, 98,100]
    
    # convert the list into string
    string = str(num_list)
    
    print("string:",string)
    print("The type of string is: ", type(string))

    Output

    string: [10, 23, 45, 68, 98, 100]
    The type of string is: <class 'str'>

    In the above example, the str() function is converting the complete list object to an str object. But if you want to convert only the elements, not the complete list object, you can use the Python map() function along with the Python str function.

    Convert a list of integers to a list of strings

    # list of integer values
    num_list = [10, 23, 45, 68, 98,100]
    
    # convert all the elements to the string value
    str_list = list(map(str, num_list))
    
    print("str_list:", str_list)
    print("The type of str_list is: ", type(str_list))

    Output

    str_list: ['10', '23', '45', '68', '98', '100']
    The type of str_list is: <class 'list'>

    Wrapping Up!

    To convert a Python integer value to a string value, we can use the str() function. The function can accept an object as a single argument and convert it into a string value. To convert back a string value to an integer value, we can use the Python int() function.

    People are also reading:

    Leave a Comment on this Post

    0 Comments