Python中如何将String转换为int
在Python中,我们可以使用int()将String转换为int。1234567891011# Stringnum1 = "88"# <class 'str'>print(type(num1))# intnum2 = int(num1)# <class 'int'>print(type(num2))例子:一个将两个数字相加的Python示例。1.1直接添加两个String。123456num1 = "1"num2 = "2"num3 = num1 + num2print(num3)输出量12例:使用int()再试一次1234567num1 = "1"num2 = "2"# convert string to intnum3 = int(num1) + int(num2)print(num3)输出量3
赞 (0)