早睡早起,方能养生!
Sleep early rise early, square can preserve one's health!

String转换为int

super
2020-09-30 19:24
views 343

在Python中,可以使用int()将String转为int

 

# string
num1 = "1"

# <class 'str'>
print(type(num1))

# int
num2 = int(num1)

# <class 'int'>
print(type(num2))

 

两个数字相加

 

num1 = "1"
num2 = "2"

num3 = num1 + num2

print(num3)
# 输出 12

# 使用int()转换
num1 = "1"
num2 = "2"

num3 = int(num1) + int(num2)
# 输出 3

分享
0 条讨论
top