在python中将Unicode字符串更改为int

时间:2013-09-26 16:23:40

标签: python unicode int

如何在python 2.7中将Unicode字符串解析为int? 我试过了:

a= int(input) 

但无法成功。

2 个答案:

答案 0 :(得分:2)

input()是python中的内置函数。它默认返回一个字符串。您可以使用外部类型转换将字符串转换为任何其他数据类型。

d = input('Enter a number')
# d will have a string value

e = int(d)
# e is now an integer value

print e * 10

a = input('Enter your name')
# a will have a string value

c = a + "is an awesome person"
# string concatination

答案 1 :(得分:1)

您可以使用try-except循环将输入转换为int。如果输入的值不正确,将抛出异常并重新提示用户输入。

while True:
     try:
          number = int(input("Enter and integer."))
     except:
          print("An integer is a WHOLE NUMBER, genius.")
     else:
          break

print(number + 10)