需求說明:
在交互式腳本中,需要用戶手動輸入內容,並對內容進行處理。在這里記錄下通過
python的內置函數input()讀取標注輸入的內容。默認的標准輸入是鍵盤。
操作過程:
1.通過input()函數接收標准輸入中的內容,然后將值賦給1變量,然后進行輸出--1行內容
str1 = input("請輸入用戶名") print(str1)
執行結果:
備注:“請輸入用戶名”,這個是提示信息。這個值是不會和鍵盤輸入的內容合並到一起賦值給變量的。通過以上的結果打印str1也只是鍵盤輸入的內容。
2.尾部的回車不會輸出
>>> input()
123456 #確認了輸入的內容之后,會有回車,但是在輸出的時候是沒有回車的。
123456
>>>
>>>
注意:
--1) 在輸入111之后,直接按回車鍵的話,就出結果了,意思就是說,input()讀取的只能是一行內容。
--2) input()如果給了提示,那么提示的字符串會打印到標准輸出即屏幕上。
--3)輸入內容,最后的那個回車,被移除了,不會輸出到屏幕上。
input()函數官方解釋:
def input(*args, **kwargs): # real signature unknown """ Read a string from standard input. The trailing newline is stripped. #指的是輸入之后,按的回車 The prompt string, if given, is printed to standard output without a trailing newline before reading input. #輸入的提示字符串,輸出在屏幕上,標准輸出,也沒有換行符,並且規則是先輸出屏幕,然后讀取標准輸入的內容。 If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. On *nix systems, readline is used if available. """ pass
文檔創建時間:2018年12月7日16:20:16