Python int to string

Python is one of the universal languages that support various types of data types like integer, decimal point number, string, and complex number. We can convert one type of data type to another data type in Python. This data type conversion process is called typecasting. In Python, an integer value can easily be converted into a string by using the str() function. The str() function takes the integer value as a parameter and converts it into the string. The conversion of int to string is not only limited to the str() function. There are various other means of int to string conversion. This article explains the int to string conversion with various methods.

Using an str() function for int to string conversion

The str() is a Python built-in function. The integer value is passed to the str() function as an argument, and it converts the given number into the string. It does not convert the original variable to the string, but it makes the string type version of the number and returns it. The syntax of the str() function is as follows:

str(integer_number)

We can determine the type of any variable by using the built-in type() function. Before converting any number into a string, we can determine the type of the variable by using the type() function. Let’s see an example of int to string conversion by using the str() function.

#declaring a number variable

num=20

#determing the type of num variable by using type() function

print("The type of variable is",type(num))

#converting the num into a string

str_value= str(num)

#determing the type of converted str_value variable by using type() function

print("The type of converted variable is",type(str_value))

Output

In the output, it can be observed that the type of converted variable is a string.

str operator

Using the %s operator for int to string conversion

The %s can be used to convert an integer into a string. The syntax of using the %s operator is as follows:

“%s” %integer

Let’s see an example of this.

#declaring an age variable

age =25

#converting the age number into a string

age_str="My age is %s"%age

#printing the age_str

print(age_str)

#checking the type of age_str variable

print(type(age_str))

Output

In the output, it can be witnessed that the type of new variable is a string.

%s strig

Using f-string for int to string conversion

The f-string mechanism can be used for int to string conversion. The syntax of using f-string is as follows:

f ‘{number}

Let’s see an example of this.

#declaring an age variable

age =25

#converting the age number into a string

age_str=f"My age is {age}"

#printing the age_str

print(age_str)

#checking the type of age_str variable

print(type(age_str))

Output

fstring

Using the format() function for int to string conversion

The format() function can be used for int to string conversion. The anticipated purpose of format() function is string formatting. While using the format() function, we put the place holder. The {} are placeholders that are used to print the value of the variable. The syntax of the format() function is:

{}”.format(number)

Let’s see an example of using the format() function for int to string conversion.

#declaring the name variable

name="Kamran"

#declaring an age variable

age =25

#using the format function for int to string conversion

print("My name is {} and my age is {}".format(name,age))

Output

format fnct

Conclusion

This article explains various methods for converting int into a string with simple examples. This article for the beginners who want to understand the Python int to string conversion.



from Linux Hint https://ift.tt/34BdSte

Post a Comment

0 Comments