将Python字符串转换为Int,将Int转换为String

  • Post author:
  • Post category:python


In this tutorial, we will learn how to convert python String to int and int to String in python. In our previous tutorial we learned about

Python List append

function.

在本教程中,我们将学习如何在python中将python String转换为int以及将int转换为String。 在上一教程中,我们了解了

Python List附加

函数。

将Python字符串转换为Int

(


Python String to Int


)

If you read our previous tutorials, you may notice that at some time we used this conversion. Actually, this is necessary in many cases. For example, you are reading some data from a file, then it will be in String format and you will have to convert String to an int.

如果您阅读了以前的教程,您可能会注意到有时我们使用了这种转换。 实际上,在许多情况下这是必要的。 例如,您正在从文件中读取某些数据,那么它将采用String格式,则必须将String转换为int。

Now, we will go straight to the code. If you want to convert a number that is represented in the string to int, you have to use

int()

function to do so. See the following example:

现在,我们将直接进入代码。 如果要将字符串中表示的数字转换为int,则必须使用

int()

函数。 请参见以下示例:

num = '123'  # string data

# print the type

print('Type of num is :', type(num))

# convert using int()

num = int(num)

# print the type again

print('Now, type of num is :', type(num))


The output of the foll