Python字符串三种格式化输出
作者:吾非同 时间:2022-12-15 09:47:59
字符串格式化输出是python非常重要的基础语法,今天就把三种格式化输出做一个简单的总结,希望对大家有帮助。
格式化输出:内容按照一定格式要求进行输出。
1.使用占位符%输出
python2.6版本之前,使用%格式化字符串沿用的是C语言的输出格式。
使用说明:
print("格式化字符串" % 变量)
#变量超过2个使用元组格式:
print("格式化字符串" % (变量1,变量2))
使用%占位符表示字符串中变量位置。
传入的值要与%占位符的变量一一对应。
其中,%s表示字符串,%d表示整数,%f表示小数(默认保留小数点后6位,%.2f保留两位小数),存在格式化标志时,需要用 %%表示一个百分号。
name='xiaoming'
age=12
print("My name is %s,My age is %d" %(name,age))
#输出:My name is xiaoming,My age is 12
2.format格式化
format是python2.6新增的一个格式化字符串的方法,相比%格式化方法有如下优点:
单个参数可以多次输出,参数顺序可以不相同
填充方式十分灵活,对齐方式十分强大
官方推荐用的方式
使用说明:
print("...{索引}, ... , {索引}, ...".format(值1, 值2))
#索引{}为空,默认按照顺序取值
print("...{key1}, ... , {key2}, ...".format(key1=value,key2=value))
name='xiaoming'
age=12
print('My name is {}, My age is {}'.format(name,age))
print('My name is {0}, My age is {1}'.format(name,age))
print('My name is {name}, My age is {age}'.format(name='xiaoming',age=12))
#输出:My name is xiaoming,My age is 12
format进阶
1.填充对齐
# 先取到值,然后在冒号后设定填充格式:{索引:[填充字符][对齐方式][宽度]}
# *<20:左对齐,总共20个字符,不够的用*号填充
print('{0:*<20}'.format('hellopython'))
# *>20:右对齐,总共20个字符,不够的用*号填充
print('{0:*>20}'.format('hellopython'))
# *^20:居中显示,总共20个字符,不够的用*号填充
print('{0:*^20}'.format('hellopython'))
输出:
hellopython*********
*********hellopython
****hellopython*****
2.位数与进制转换
#保留2位有效数字
print("{:.2f}".format(3.1415926))
#转成二进制
print('{0:b}'.format(16))
#转成八进制
print('{0:o}'.format(10))
#转成十六进制
print('{0:x}'.format(15))
输出
3.14
10000
12
f
f-string格式化
在Python 3.6中引入 了f-strings,不仅比str.format使用简单,而且效率也更高。
使用说明
f-string是字符串前面加上 "f",{}直接使用变量、表达式等。
name='xiaoming'
age=12
#{}中直接使用变量
print(f'My name is {name},My age is {age}')
#{}中运行表达式
print(f'{1+2+3}')
#调用Python内置函数
print(f'{name.upper()}')
#用lambda匿名函数:可以做复杂的数值计算
fun = lambda x : x+1
print(f'{fun(age)}')
#输出
My name is xiaoming,My age is 12
6
XIAOMING
13
来源:https://blog.csdn.net/XingLongSKY/article/details/108426836
标签:Python,字符串,格式化,输出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python画环形图的方法
2023-02-12 09:54:24
![](https://img.aspxhome.com/file/2023/2/66742_0s.jpg)
Python之Pygame的Draw绘图
2022-11-29 18:51:18
《悟透JavaScript》感谢语
2008-11-12 12:59:00
![](https://img.aspxhome.com/file/UploadPic/200811/12/covera-92s.jpg)
Python基于Opencv来快速实现人脸识别过程详解(完整版)
2023-09-06 17:04:25
![](https://img.aspxhome.com/file/2023/7/76857_0s.jpg)
提升设计品质的8个布局方案[译]
2010-03-18 16:06:00
![](https://img.aspxhome.com/file/UploadPic/20103/18/01-33s.jpg)
怎样在SQL Server 2005中用证书加密数据
2009-01-04 14:11:00
Python后台管理员管理前台会员信息的讲解
2023-11-06 09:59:41
Asp WinHttp.WinHttpRequest.5.1 对象使用详解
2012-05-02 10:15:27
Python开发之Nginx+uWSGI+virtualenv多项目部署教程
2023-07-30 04:42:39
![](https://img.aspxhome.com/file/2023/9/61689_0s.jpg)
sql数据库批量处理脚本
2011-11-03 17:23:19
prototype.js常用函数及其用法
2007-11-21 20:47:00
SqlServer表死锁的解决方法分享
2011-09-30 11:16:16
超酷的js图片轮播渐变效果
2007-10-10 20:45:00
![](https://img.aspxhome.com/file/UploadPic/200710/10/2007101021445381s.jpg)
python时间日期操作方法实例小结
2021-03-13 11:01:45
Yahoo! BrowserPlus 介绍
2008-06-01 16:38:00
Python实现简易端口扫描器代码实例
2022-08-07 16:58:24
![](https://img.aspxhome.com/file/2023/5/84895_0s.png)
服务器XMLHTTP(Server XMLHTTP in ASP)基础
2008-11-11 12:45:00
ASP运行在IIS6 500错误解决办法
2011-04-19 11:15:00
![](https://img.aspxhome.com/file/UploadPic/20112/5/201125111853302s.gif)
Oracle 11g安装错误提示未找到wfmlrsvcapp.ear的解决方法
2023-07-14 14:31:52
![](https://img.aspxhome.com/file/2023/6/64466_0s.jpg)
Oracle学习笔记(五)
2012-01-05 18:52:30