How do i convert an int to a string in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string. But use of the str() is not the only way to do so. This type of conversion can also be done using the “%s” keyword, the .formatfunction or using f-stringfunction.

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

    1. Using str() function 

    Syntax: str(integer_value)

    Example:  

    Python3

    num = 10

    print("Type of variable before convertion : ", type(num)) 

    converted_num = str(num)

    print("Type After convertion : ",type(converted_num))

    Output:

    Type of variable before convertion :  <class 'int'>
    Type After convertion :  <class 'str'>

    2. Using “%s” keyword

    Syntax: “%s” % integer

    Example: 

    Python3

    num = 10

    print("Type of variable before convertion : ", type(num)) 

    converted_num = "% s" % num

    print("Type after convertion : ", type(converted_num))

    Output:

    Type of variable before convertion :  <class 'int'>
    Type after convertion :  <class 'str'>

    3. Using .format() function

    Syntax: ‘{}’.format(integer)

    Example: 

    Python3

    num = 10

    print("Type before convertion : ", type(num)) 

    converted_num = "{}".format(num)

    print("Type after convertion :",type(converted_num))

    How do i convert an int to a string in python?

    Output:

    Type before convertion :  <class 'int'>
    Type after convertion : <class 'str'>

    4. Using f-string

    Syntax: f'{integer}’

    Example: 

    Python3

    num = 10

    print("Type before convertion : ",type(num)) 

    converted_num = f'{num}'

    print("Type after convetion : ", type(converted_num))

    Output:

    Type before convertion :  <class 'int'>
    Type after convetion :  <class 'str'>

                                                                                                                        5. Using __str__() method 

    Syntax:  Integer.__str__()

    Python3

    num = 10

    print("Type before convertion : ",type(num)) 

    converted_num = num.__str__()

    print("Type after convetion : ", type(converted_num))

    Output:

    Type before convertion :  <class 'int'>
    Type after convetion :  <class 'str'>

    How do you convert an integer to a string?

    Java int to String Example using String.valueOf().
    public class IntToStringExample1{.
    public static void main(String args[]){.
    int i=200;.
    String s=String.valueOf(i);.
    System.out.println(i+100);//300 because + is binary plus operator..
    System.out.println(s+100);//200100 because + is string concatenation operator..

    How do you convert an int to a string in Python 3?

    We can convert numbers to strings using the str() method. We'll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value.

    How do you convert int to string without using library functions in Python?

    Convert int to string python without str() function Simple example code keeps dividing a given int value by 10 and prepending the remainder to the output string. Use the ordinal number of '0' plus the remainder to obtain the ordinal number of the remainder, and then convert it to string using the chr function.

    How do you convert int to string manually?

    Conversion of an integer into a string by using to_string() method. The to_string() method accepts a single integer and converts the integer value or other data type value into a string.