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]