How to convert int to string in Python

How to convert int to string in Python

We can convert an integer data type using the Python built-in str() function. This function takes any data type as an argument and converts it into a string. But we can also do it using the “%s” literal and using the .format() function. Below is the syntax of the str() function.

Syntax –

Let’s understand the following example.

Example – 1 Using the str() function

Output:

<class 'int'>
25
<class 'str'>
25

Example – 2 Using the “%s” integer

Output:

<class 'int'>
<class 'str'>

Example – 3: Using the .format() function

Output:

<class 'int'>
<class 'str'>

Example – 4: Using f-string

Output:

<class 'int'>
<class 'str'>

We have defined all methods of converting the integer data type to the string type. You can use one of them according to your requirement.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263626.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论