• 技术文章 >常见问题 >Python常见问题

    python如何获取键盘输入

    silencementsilencement2019-10-28 10:38:27原创10632


    python2中:raw_input函数

    raw_input() 函数从标准输入读取一个行,并返回一个字符串(去掉结尾的换行符):

    str = raw_input("Enter your input: ");
    print "Received input is : ", str

    这将提示你输入任意字符串,然后在屏幕上显示相同的字符串。当我输入"Hello Python!",它的输出如下:

    Enter your input: Hello Python
    Received input is :  Hello Python

    Python3中:input函数

    input() 函数和raw_input() 函数基本可以互换,但是input会假设你的输入是一个有效的Python表达式,并返回运算结果。这应该是两者的区别。

    str = input("Enter your input: ");
    print "Received input is : ", str

    这会产生如下的对应着输入的结果:

    Enter your input: [x*5 for x in range(2,10,2)]
    Recieved input is :  [10, 20, 30, 40]

    注意事项:

    因为input()会把输入当成python脚本来处理,所以如果输入字符串类型的变量,要记得加引号,如‘abc’!更多学习内容,请点击Python学习网

    专题推荐:input
    品易云
    上一篇:字符串切片的方法是什么 下一篇:python定义函数不带括号吗

    相关文章推荐

    • Python input()函数:获取用户输入的字符串• python里input怎么解释• Python的input能干什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网