Python起步之print & input用法总结

来源:互联网 发布:在中国卖的好跑车知乎 编辑:程序博客网 时间:2024/05/21 08:45

          前言

                         “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也可以,接下来笔者也将一一的

                 去尝试。

                          1、输出整数。
                                这里笔者参考了网上的格式化输出,但是按照我的输出报错,经过调整是少了一层括
                           号的问题。                              
>>> print("the length of (%s) is %d" %('Python',len('python')),end="!")the length of (Python) is 6!
                          2、其他进制数。
                                 各个进制数的占位符形式:
                                  %x--- hex 十六进制
                                  %d---dec 十进制
                                  %o---oct   八进制

>>> number=15>>> print("dec-十进制=%d\noct-八进制=%o\nhex-十六进制=%x" % (number,number,number))dec-十进制=15oct-八进制=17hex-十六进制=f
                          3、输出字符串
>>> 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>字符串长度时,后面不用空格占位


                            4、输出浮点数(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的输入输出就总结到这里了!