zoukankan      html  css  js  c++  java
  • Python读取键盘输入

    Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘。例如以下:

    • raw_input
    • input

    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

    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]

  • 相关阅读:
    Python深拷贝和浅拷贝解析
    python中count函数的用法
    Jenkins + gitlab + maven 自动打包部署项目
    nio和bio得区别
    nginx负载均衡的5种策略
    接口测试常见bug
    接口自动化面试4
    pass 语句
    if 语句
    while循环
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/7261721.html
Copyright © 2011-2022 走看看