Python Study

时间:2021-06-02 14:20:49   收藏:0   阅读:0

输入语句

python中,通过input函数获取用户键盘输入

input函数的参数是字符串,它是屏幕提示语

# 变量赋值,=两边空格可有可无
>>> user = input(username: )   # 用户输入内容保存到变量user中
username: tom
>>> user    # 使用变量
tom

# input读入的数据都是字符串类型。相同类型的数据才能运算
>>> num = input("number: ")
number: 10
>>> num + 5    # num是字符串,不能和数字进行运算
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int
>>> type(num)    # 查看num的类型
<class str>
>>> int(num) + 5  # int函数将字符串转成整数
15
>>> num + str(5)  # str函数将对象转成字符串
105
>>> num + "5"   #用引号都会变成字符串
105
 
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!