Python起步之print & input用法總結

          前言

                         「Hello World」算是編程語言中的經典了吧,我相信每一個程序員都是從Hello world起步的。
                 一句簡單的"Hello World"表達了Coder對世界的問候。小生一直以爲Coder是一羣不善言談,
                 心裏情感豐富的可愛的人。哦,有點跑題了,此篇文章筆者將對Python中的print 、input作一個
                 簡單的總結。看看Python是如何處理輸入輸出的。

          print函數

                        經過名字就能夠知道這是輸出函數,那麼它是如何使用的,咱們如何藉助它來實現漂亮的
                 輸入輸出呢?接下來筆者將一一嘗試。

              help(print)

                        在實踐以前咱們先看看在交互式解釋器中使用help(print)查看print函數的使用簡介吧。
                  這裏我使用的是Python3.3安裝以後自帶的工具IDLE,若是想經過cmd實現help命令的話,須要
                  配置好環境變量。
                         打開IDLE輸入 help(print),咱們能夠看到以下結果:
>>> help(print) Help on built-in function print in module builtins:  print(...)     print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)          Prints the values to a stream, or to sys.stdout by default.     Optional keyword arguments:     file:  a file-like object (stream); defaults to the current sys.stdout.     sep:   string inserted between values, default a space.     end:   string appended after the last value, default a newline.     flush: whether to forcibly flush the stream.  
                      從描述中能夠看出的是print是支持不定參數的,默認輸出到標準輸出,並且不清空緩存。
                各個參數之間默認以空格分隔。輸出以一個換行符結束。
                      看看一個簡單的輸出吧:    
>>> print("hello","Pyhton",sep="--",end="...");print("!") hello--Pyhton...!
                     經過help命令咱們能夠很清楚的明白print函數的參數列表,這對於咱們對print的認識是有
               幫助的。

            格式化輸出

                         咱們知道C語言中能夠實現格式化的輸出,其實Python也能夠,接下來筆者也將一一的python

                 去嘗試。程序員

                          一、輸出整數。
                                這裏筆者參考了網上的格式化輸出,可是按照個人輸出報錯,通過調整是少了一層括
                           號的問題。                              
>>> print("the length of (%s) is %d" %('Python',len('python')),end="!") the length of (Python) is 6!
                          二、其餘進制數。
                                 各個進制數的佔位符形式:
                                  %x--- hex 十六進制
                                  %d---dec 十進制
                                  %o---oct   八進制

>>> number=15 >>> print("dec-十進制=%d\noct-八進制=%o\nhex-十六進制=%x" % (number,number,number)) dec-十進制=15 oct-八進制=17 hex-十六進制=f
                          三、輸出字符串
>>> print ("%.4s " % ("hello world")) hell 
 
>>> print("%5.4s" %("Hello world"))  Hell
                            這裏輸出結果爲「 Hello」,前面有一個空格
                            一樣對其具體的輸出格式也能夠寫成以下的形式
>>> print("%*.*s" %(5,4,"Hello world"))  Hell
                              這裏對Python的print字符串格式輸出形式
                                     %A.Bs:A表示輸出的總的字符串長度
                                     B表示要輸出字符串從開始截取的長度
                                     A<B的時候,輸出字符串長度爲B(A能夠<0 )
                                     A>B的時候前方空格
                                     B>字符串長度時,後面不用空格佔位


                            四、輸出浮點數(float)       
>>> print("%10.3f" % 3.141516)      3.142
                                  浮點數的輸出控制和字符串類似,不過序注意的是.3表示的輸出3位小數, 最後一面按四
                            舍五入方式進位。

              input函數

                            瞭解了輸出函數以後咱們來看看輸入函數input,一樣的咱們先在交互式解釋器中查看input
                        函數的具體狀況。
input(...)     input([prompt]) -> string          Read a string from standard input.  The trailing newline is stripped.     If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.     On Unix, GNU readline is used if enabled.  The prompt string, if given,     is printed without a trailing newline before reading.  >>> 
                          注意這裏筆者的Python是3.3的,根據上面的描述能夠很清楚的看到,input函數是從標準輸入流
                      讀取一個字符串,注意換行符是被剝離的。 input能夠有一個參數,用來提示輸入信息的,下面具體
                      例子:
>>> name = input("your name is:") your name is:kiritor >>> print(name) kiritor >>> 
                      查看官方文檔咱們能夠更清楚的明白input是如何工做的。                
If the prompt argument is present, it is written to standard output without a trailing newline.   The function then reads a line from input, converts it to a string (stripping a trailing newline),   and returns that. When EOF is read, EOFError is raised. Example:
                      ok,對於Python的輸入輸出就總結到這裏了!
相關文章
相關標籤/搜索