栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python中print()输出函数用法详解

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python中print()输出函数用法详解

Python中print()输出函数用法详解

1.直接输出字符串
>>>print(‘Hello World!’)
Hello World!

>>>print("abcd")
abcd

>>>print('Hello','World!')
Hello World!

>>>pritn("Hello"+"World!")
HelloWorld!

2.输出变量

        可以输出各种类型的变量

>>>a = 'hello'
>>>print(a)
hello
>>>b = 123
>>>print(b)
123
>>>c = 1.5
>>>print(c)
1.5
文本类型:str
数值类型:int, float, complex
序列类型:list, tuple, range
映射类型:dict
集合类型:set, frozenset
布尔类型:bool
二进制类型:bytes, bytearray, memoryview
3.进行运算
>>>print(1+1) #加
2
>>>ptint(2-1) #减
1
>>>print(2*2) #乘
4
>>>print(6/2) #除
3.0
>>>print(5%2) #模除
1
>>>print(2**3) #乘方
8

4.数据格式化后输出 %字符
>>>a = 'Python'
>>>b = 'This'
>>>print('%s is %s World' %(a,b)) 
This is Python World

>>>c = len(a)
>>>print("'%s' is %d long" %(a,c))
'Python' is 6 long

%s  字符串采用str()的显示

%x 十六进制整数

%r  字符串(repr())的显示

%e 指数(基底写e)

%c  单个字符    

%E   指数(基底写E)

%b 二进制整数                                  

%f,%F 浮点数

%d 十进制整数                                  

%g   指数(e)或浮点数(根据显示长度)

%i  十进制整数                                  

 %G  指数(E)或浮点数(根据显示长度)

%o 八进制整数                                    

%%  字符%

5.print()参数

        (1)value参数

                不用说了吧,所见即所得,亮实例!

>>>print('hello')
hello
>>>print('hello','world')
hello world
>>>print('hello','python','world')
hello python world

        (2)sep参数

                 间隔符:一次print多个值时作间隔

>>>print('hello','world') #默认sep=' '
hello world
>>>print('hello','world',sep='')
helloworld
>>>print('hello','world',sep='$')
hello$world
>>>print('hello''world') #也可以去掉逗号来取消间隔
helloworld

        (3)end参数

                结束符:打印结束后要干什么

>>>print('hello,world') #默认是有换行符的
helli,world
>>>print('hello',end='') #打印结束后空字符串
hello>>>print('',end='n') #打印换行符

>>>print('world',end='~')
world~

               这里n是换行符的转义字符

n

换行符,将光标位置移到下一行开头。
r回车符,将光标位置移到本行开头。
t水平制表符,也即 Tab 键,一般相当于四个空格。
a蜂鸣器响铃。注意不是喇叭发声,现在的计算机很多都不带蜂鸣器了,所以响铃不一定有效。
b退格(Backspace),将光标位置移到前一列。
\反斜线
'单引号
"双引号
在字符串行尾的续行符,即一行未完,转到下一行继续写。

         (4)file参数()

                流重定向,先来看一个实例

fout = open('./test.txt','w') #创建输出对象
print("HelloWorld",file=fout) #file参数就是流重定向
fout.close() #消除对象,不占用内存

       

                 不难发现,file参数将‘hello’输出到了test.txt文件中。

        (5)flush参数

                刷新:有亿点复杂…先来看flush的原理。

                 首先,用户发出命令,执行源代码,一次传一行个python解释器,将源代码解释为机器语言,存入RAM/ROM/Cache 缓存,在发送给CPU执行。这时,当flush=False时,会回到源代码,再传一行个解释器,再执行,直到所有源代码都执行完,发送给显示器,反馈结果给用户。当flush=True时,每执行一行,就显示一行,实时刷新。

                如下面的例子,print到f中的内容先存到内存中,当文件对象关闭时才把内容输出到 test.txt 中;而当flush = True时它会立即把内容刷新存到 test.txt 中。

f = open('./test.txt','w')
print('hello',file=f,flush=True)
f.close()

                这个功能在客户端脚本几乎用不上, 大多用于服务器端              

                比如在线web聊天,页面会实时显示聊天的内容, 其实后台是一直在向服务器请求数据的, 正常情况下是请求完毕之后才会输出相应内容, 但是即时聊天,需要一有响应就得立即返回, flush也就起作用了。

6.print技巧

        (1)如果想要打印10个a,可以这样

print('aaaaaaaaaa')
print('a'*10)
for i in range(10):
    print('a',end='')

等等等等

        (2)format

#将format后面的内容以此填充
>>>print "My name is {0} and I am  {1}".format("xiaoming",20)     
My name is xiaoma and I am  20

#{}里面相当于一个变量
>>>print "My name is {name}".format(name="xiaoming")    
My name is xiaoma

        (3)输出乘法表

        

for i in range(1, 10):
    print
    for j in range(1, i+1):
        print "%d*%d=%d" % (i, j, i*j),

1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

文章制作不易,喜欢的话就点个赞呗!

一个IT方面的QQ群:690491195有兴趣的加~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325904.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号