Python中的print()函数用法总结
文章目录
- print()函数用法:
2.格式化输出:
3.Python常用格式字符: - 输出列表元素
- join()函数:
- fomat()函数:
- print()函数用法:
函数语法:
print(*objects, sep=' ', end='\n', file=sys.stdout)
1
objects – 复数,表示可以一次输出多个对象。输出多个对象时,需要用 , 分隔。
sep – 用来间隔多个对象,默认值是一个空格。
end – 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符串。
file – 要写入的文件对象。
2.格式化输出:
在C语言中,我们可以使用printf("%-.4f",a)之类的形式,实现数据的的格式化输出。
在python中,我们同样可以实现数据的格式化输出。
s = 'Duan Yixuan'
x = len(s)
print('The length of %s is %d' % (s,x))
和C语言的区别在于,Python中格式控制符和转换说明符用%分隔,C语言中用逗号。
1
2
3
4
'
运行运行
设置宽度与精度:
PI = 3.141592653
print('%10.3f' % PI) #字段宽10,精度3
输出: 3.142
精度为3,所以只显示142,指定宽度为10,所以在左边需要补充5个空格,以达到10位的宽度
1
2
3
4
5
'
运行运行
转换标志:-表示左对齐;+表示在数值前要加上正负号;" "(空白字符)表示正数之前保留空格();0表示转换值若位数不够则用0填充。
PI = 3.1415926
print('%-10.3f' % PI) #左对齐,还是10个字符,但空格显示在右边。
3.142
1
2
3
4
'
运行运行
PI = 3.1415926
print('%+f' % PI) #显示正负号 #+3.141593
类型f的默认精度为6位小数。
1
2
3
'
运行运行
PI = 3.1415926
print('%010.3f' % PI) #字段宽度为10,精度为3,不足处用0填充空白
000003.142 0表示转换值若位数不够则用0填充
1
2
3
'
运行运行
3.Python常用格式字符:
%s 字符串采用str()的显示
%x 十六进制整数
%r 字符串(repr())的显示
%e 指数(基底写e)
%c 单个字符
%E 指数(基底写E)
%b 二进制整数
%f,%F 浮点数
%d 十进制整数
%g 指数(e)或浮点数(根据显示长度)
%i 十进制整数
%G 指数(E)或浮点数(根据显示长度)
%o 八进制整数
%% 字符%
- 输出列表元素
l = [1,2,3,4]
for i in l:
print(i)
一行一个,自动换行
1
2
3
4
'
运行运行
l = [1,2,3,4]
for i in l:
print(i,end=' ')
以空格为分隔符,不换行,但是最后有空格
1
2
3
4
'
运行运行
l = [1,2,3,4]
print(" ".join(str(i) for i in l))
以空格为分隔符,最后无空格
1
2
3
'
运行运行
for x in list:
print(x, end=' ' if x != list[-1] else '')
判断该元素是不是列表的最后一个元素,根据判断结果输出分隔符
1
2
3
a = list()
print(*a,sep=' ')
一行输出列表
1
2
3
'
运行运行
5. join()函数:
将一个包含多个字符串的可迭代对象,转为用分隔符s连接的字符(不能是数字)
a = ["he", "l", "l", "o"]
print(" ".join(a))
输出:he l l o
注意最后没有空格
1
2
3
4
'
运行运行
6. fomat()函数:
1.通过位置来填充字符串
print('hello {0} i am {1}'.format('world','python'))
输入结果:hello world i am python
print('hello {} i am {}'.format('world','python') )
输入结果:hello world i am python
print('hello {0} i am {1} . a now language-- {1}'.format('world','python')
输出结果:hello world i am python . a now language-- python
1
2
3
4
5
6
foramt会把参数按位置顺序来填充到字符串中,第一个参数是0,然后1 ……
也可以不输入数字,这样也会按顺序来填充
同一个参数可以填充多次,这个是format比%先进的地方
2.通过key来填充
obj = 'world'
name = 'python'
print('hello, {obj} ,i am {name}'.format(obj = obj,name = name))
输入结果:hello, world ,i am python
1
2
3
4
'
运行运行
3.通过列表填充
list=['world','python']
print('hello {names[0]} i am {names[1]}'.format(names=list))
输出结果:hello world i am python
print('hello {0[0]} i am {0[1]}'.format(list))
输出结果:hello world i am python
1
2
3
4
5
'
运行运行
4.通过字典填充
dict={‘obj’:’world’,’name’:’python’}
print(‘hello {names[obj]} i am {names[name]}’.format(names=dict))
hello world i am python
注意访问字典的key,不用引号的
1
2
3
4
5.通过类的属性填充
class Names():
obj='world'
name='python'
print('hello {names.obj} i am {names.name}'.format(names=Names))
输入结果hello world i am python
1
2
3
4
5
'
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?