开放的自由多可贵 發表於 2019-10-9 14:20:00

python中print用法

<p>print用法</p>
<p>参考文档:<br>https://blog.csdn.net/sinat_28576553/article/details/81154912</p>
<p>&nbsp;</p>
<p>目录</p>
<p>一、print()函数概述</p>
<p>二、变量的输出</p>
<p>三、数据的格式化输出</p>
<p>3.1 %字符</p>
<p>3.2&nbsp;最小字段宽度和精度</p>
<p>3.3&nbsp;转换标志</p>
<p>3.4&nbsp;格式字符归纳</p>
<p>四、换行与防止换行</p>
<p>一、print()函数概述<br>print()&nbsp;方法用于打印输出,是python中最常见的一个函数。</p>
<p>该函数的语法如下:</p>
<p>print(*objects, sep=' ', end='\n', file=sys.stdout)<br>参数的具体含义如下:</p>
<p>objects --表示输出的对象。输出多个对象时,需要用 , (逗号)分隔。</p>
<p>sep -- 用来间隔多个对象。</p>
<p>end -- 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符。</p>
<p>file -- 要写入的文件对象。</p>
<p>print(1)#数值类型可以直接输出<br> <br>'''<br>运行结果如下<br>1<br>'''<br> <br>print("Hello World")#字符串类型可以直接输出<br> <br>'''<br>运行结果如下:<br>Hello World<br>'''<br> <br>a=1<br>b="Hello World"<br>print(a, b)#可以一次输出多个对象,对象之间用逗号分隔<br>'''<br>运行结果如下:<br>1 Hello World<br>'''<br> <br>#如果直接输出字符串,而不是用对象表示的话,可以不使用逗号<br>print("Duan""Yixuan")<br>print("Duan","Yixuan")<br> <br>'''<br>运行结果如下:<br>DuanYixuan<br>Duan Yixuan<br>可知,不添加逗号分隔符,字符串之间没有间隔<br>'''<br> <br>print("www", "snh48", "com", sep=".")# 设置间隔符<br>'''<br>运行结果如下:<br>www.snh48.com<br>'''<br>二、变量的输出<br>无论什么类型的数据,包括但不局限于:数值型,布尔型,列表变量,字典变量...都可以直接输出。</p>
<p> <br>#例如:<br> <br>num = 19<br>print(num)    #19输出数值型变量<br> <br>str = 'Duan Yixuan'<br>print(str)#Duan Yixuan输出字符串变量<br> <br>list = <br>print(list)   #输出列表变量<br> <br>tuple = (1,2,'a')<br>print(tuple)    #(1, 2, 'a') 输出元组变量<br> <br>dict = {'a':1, 'b':2}<br>print(dict)   # {'a': 1, 'b': 2} 输出字典变量<br>三、数据的格式化输出<br>在C语言中,我们可以使用printf("%-.4f",a)之类的形式,实现数据的的格式化输出。</p>
<p>在python中,我们同样可以实现数据的格式化输出。我们可以先看一个简单的例子:</p>
<p>s='Duan Yixuan'<br>x=len(s)<br>print('The length of %s is %d' %(s,x))<br> <br>'''<br>'The length of %s is %d' 这部分叫做:格式控制符<br>(s,x) 这部分叫做:转换说明符<br>% 字符,表示标记转换说明符的开始<br>输出如下:<br>The length of Duan Yixuan is 11<br>'''<br>和C语言的区别在于,Python中格式控制符和转换说明符用%分隔,C语言中用逗号。</p>
<p>接下来我们仔细探讨一下格式化输出</p>
<p>3.1 %字符<br>(1).%字符:标记转换说明符的开始。</p>
<p>%字符的用法可参考上例,不再赘述。</p>
<p>3.2&nbsp;最小字段宽度和精度<br>最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*(星号),则宽度会从值元组中读出。</p>
<p>点(.)后跟精度值:如果需要输出实数,精度值表示出现在小数点后的位数。如果需要输出字符串,那么该数字就表示最大字段宽度。如果是*,那么精度将从元组中读出。</p>
<p>可参考C语言的实现方式。</p>
<p>注:字段宽度中,小数点也占一位。</p>
<p>PI = 3.141592653<br>print('%10.3f'%PI)#字段宽10,精度3<br>#   3.142<br> <br>#精度为3,所以只显示142,指定宽度为10,所以在左边需要补充5个空格,以达到10位的宽度<br>PI=3.1415926<br>print("PI=%.*f"%(3,PI))<br>#用*从后面的元组中读取字段宽度或精度,可以读取出来精度是3位<br>#PI=3.142 <br> <br>#没有指定宽度,所以不需要缩进<br> <br>print("PI=%*.3f"%(10,PI))   #精度为3,总长为10.<br># PI=   3.142<br> <br>#* 所处的位置不同,读取的内容也不同<br>3.3&nbsp;转换标志<br>转换标志:-表示左对齐;+表示在数值前要加上正负号;" "(空白字符)表示正数之前保留空格();0表示转换值若位数不够则用0填充。</p>
<p>具体的我们可以看一下例子:</p>
<p>PI=3.1415926<br>print('%-10.3f' %PI)#左对齐,还是10个字符,但空格显示在右边。<br>#3.142<br>PI=3.1415926<br>print('%+f' % PI)#显示正负号#+3.141593<br># 类型f的默认精度为6位小数。<br>PI=3.1415926<br>print('%010.3f'%PI) #字段宽度为10,精度为3,不足处用0填充空白<br>#000003.142   0表示转换值若位数不够则用0填充<br>3.4&nbsp;格式字符归纳<br>格式字符&nbsp;&nbsp;&nbsp; 说明&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;格式字符&nbsp;&nbsp;&nbsp;&nbsp; 说明</p>
<p>%s&nbsp; 字符串采用str()的显示&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;%x 十六进制整数</p>
<p>%r&nbsp; 字符串(repr())的显示&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; %e 指数(基底写e)</p>
<p>%c&nbsp; 单个字符&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; %E&nbsp;&nbsp; 指数(基底写E)</p>
<p>%b 二进制整数&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;%f,%F&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 浮点数</p>
<p>%d 十进制整数&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;%g&nbsp;&nbsp; 指数(e)或浮点数(根据显示长度)</p>
<p>%i&nbsp; 十进制整数&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; %G&nbsp; 指数(E)或浮点数(根据显示长度)</p>
<p>%o 八进制整数&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;%%&nbsp; 字符%</p>
<p>四、换行与防止换行<br>在python中,输出函数总是默认换行,比如说:</p>
<p>for x in range(0,5):<br>    print(x)<br> <br>'''<br>0<br>1<br>2<br>3<br>4<br>'''<br>而显然,这种输出太占“空间”,我们可以进行如下改造:</p>
<p>参考文本第一部分对end参数的描述:end -- 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符。</p>
<p>for x in range(0, 5):<br>    print(x, end=' ')<br>#0 1 2 3 4 <br>for x in range(0, 5):<br>    print(x, end=',')<br>#0,1,2,3,4,<br>但如果,我们同时运行上面两段代码,结果会如下所示,可知:我们需要在两次输出间,实现换行。</p>
<p>for x in range(0, 5):<br>    print(x, end=' ')<br>for x in range(0, 5):<br>    print(x, end=',')<br> <br>#0 1 2 3 4 0,1,2,3,4,<br>我们比较以下几种方式</p>
<p>方式一:</p>
<p>for x in range(0, 5):<br>    print(x, end=' ')<br> <br>print('\n')<br> <br>for x in range(0, 5):<br>    print(x, end=',')<br> <br>'''<br>0 1 2 3 4 <br>0,1,2,3,4,<br>'''<br>之所以出现上面这种情况,是因为print()本身就是默认换行的,再加上换行符,相当于换行两次。</p>
<p>方式二:</p>
<p>for x in range(0, 5):<br>    print(x, end=' ')<br> <br>print()#本身自带换行,完美输出<br> <br>for x in range(0, 5):<br>    print(x, end=',')<br> <br>'''<br>0 1 2 3 4 <br>0,1,2,3,4,<br>'''</p><br><br>
来源:https://www.cnblogs.com/yxqnote/p/11641555.html
頁: [1]
查看完整版本: python中print用法