python格式化字符串的实战教程(使用占位符、format方法)
作者:HHYZBC 时间:2023-02-01 22:20:21
格式化字符串是什么?
Python字符串的格式化处理主要是用来将变量(对象)的值填充到字符串中,在字符串中解析Python表达式,对字符串进行格式化显示(左对齐、右对齐、居中对齐,保留数字有效位数)。
1.使用占位符格式化字符串:
使用占位符格式化输出时:在%后面加数字表示给这个字符多少个位置,不足电脑会自动使用空格补齐。正数表示左对齐,负数表示右对齐。如:%4d表示左对齐一共占4的位置,%-8d表示右对齐一共占8个位置
占位符:
%s :字符串的格式化,也是最常用的
%d :格式化整数,也比较常用
%c :格式化字符及ASCII码
%f :格式化浮点数,可以指定小数后面的精度,默认是小数点6位
%o :格式化无符号八进制数
%x :格式化无符号十六进制数
%e : 将整数、浮点数转换成科学计数法
%%: 当字符串中存在格式化标志时,需要用 %%表示一个百分号;
例子:
占位符%s、%c、%%的使用:
name='小明'
print('%s的使用:')
print('我的名字叫%s!'%(name))
print('我的名字叫%4s!'%(name)) #右对齐
print('我的名字叫%-4s!'%(name)) #左对应
#由于在python中只有字符串这种类型,使用在这里%c只能表示ASCII码,并不能表示字符
character=56
print('c的使用:')
print('%d在ASCII码中表示字母%c'%(character,character))
schedule=99
print('%%的使用:')
print('当前下载进度为%d%%!'%(character))
由于在python中只有字符串这种类型,所以在python中%c只能表示ASCII码,并不能表示字符 !
占位符%d、%o、%x的使用:
number=18
print('%d使用十进制为:%d'%(number,number)) #十进制
print('%d使用八进制为:%o'%(number,number)) #八进制
print('%d使用十六进制为:%x'%(number,number)) #十六进制
如果在表示八进制或者十六进制时想显示各进制的前缀时,在%后面加一个#就好:
number=200
print('%d使用八进制为:%#o'%(number,number))
print('%d使用十六进制为:%#x'%(number,number))
占位符 %f、%e的使用:
high=174.5
print('我的身高有%f米'%(high)) #默认情况
print('我的身高有%.1f米'%(high)) #小数点保留1位
print('%f使用科学记数法为:%e'%(high,high))
2.使用format方法格式化输出:
1.不设置指定位置:
name = "{}在{}玩了一天的{}"
data = name.format("小明","网吧","LOL")
print(data)
2.设置指定名称:
name = "{name}在{Location}玩了一天的{game}"
data = name.format(Location="网吧",game="LOL",name="小明")
print(data)
3.设置指定位置 :
name = "{2}在{0}玩了一天的{1}"
data = name.format("网吧","LOL","小明",)
print(data)
注意:指定位置是从0开始算起,而不是1!
来源:https://blog.csdn.net/HHYZBC/article/details/123380930
标签:python,字符串,格式化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
数据库中聚簇索引与非聚簇索引的区别[图文]
2012-02-25 19:38:23
Django实现上传图片功能
2022-01-06 13:57:44
![](https://img.aspxhome.com/file/2023/4/66364_0s.jpg)
debug case: onsubmit=return false依然能提交
2009-01-18 13:07:00
golang实践-第三方包为私有库的配置方案
2023-06-25 06:05:32
![](https://img.aspxhome.com/file/2023/3/92033_0s.jpg)
很有意思的SQL多行数据拼接
2011-11-03 17:08:29
python numpy数组中的复制知识解析
2023-08-10 14:41:33
谈谈网页设计中的字体应用 (4) 实战应用篇·下
2009-11-24 13:13:00
![](https://img.aspxhome.com/file/UploadPic/200911/24/202939796-23s.gif)
Symfony核心类概述
2023-11-17 13:59:48
![](https://img.aspxhome.com/file/2023/7/92807_0s.png)
ODBC的保留字有哪些?
2009-10-28 18:34:00
python3图片转换二进制存入mysql
2023-05-18 07:06:50
python实现维吉尼亚加密法
2023-06-12 23:19:21
![](https://img.aspxhome.com/file/2023/7/59817_0s.jpg)
Python爬虫实现百度图片自动下载
2021-07-12 22:42:56
![](https://img.aspxhome.com/file/2023/3/65423_0s.png)
Django项目中使用JWT的实现代码
2023-03-05 09:21:57
![](https://img.aspxhome.com/file/2023/2/70022_0s.png)
两个css郁闷的发现
2007-12-16 15:31:00
MySQL数据库的23个特别注意事项
2010-08-08 14:43:00
PHP编程文件处理类SplFileObject和SplFileInfo用法实例分析
2023-11-17 00:16:13
PHP中quotemeta()函数的用法讲解
2023-06-08 04:04:24
![](https://img.aspxhome.com/file/2023/7/55377_0s.png)
oracle12C安装步骤(图文详解)
2023-07-15 14:19:34
![](https://img.aspxhome.com/file/2023/3/64093_0s.png)
如何做网页设计的10个小窍门
2010-04-11 22:30:00
Python基础之常用库常用方法整理
2022-10-30 10:43:26
![](https://img.aspxhome.com/file/2023/7/63897_0s.png)