Python基础09-字符串格式化

news/2024/7/3 2:32:12

字符串格式化。主要是%格式,format格式化方法,具体写在代码例子的注释里。

msg = list()
# %s 接收字符串
msg.append("i am %s, which is a database." % "mysql")
msg.append("i am %s, which is a %s." % ("db2", "database"))
msg.append("i am %s, which is a %s, %s." % ("oracle", "database", "too"))
msg.append("these are database, %s" % (["mysql", "db2", "oracle"]))name = "Kevin"
age = 29
# %s可以接收任意变量,将其转换为字符串。这样做可读性差,建议还是根据数据类型用不同的%
msg.append("i am %s, age %s." % (name, age))
msg.append("i am %s, age %d." % (name, age))
msg.append("percent is %s" % 99.769273703747)# %f 接收浮点数,默认保留6位小数。# percent is 99.769273703747
msg.append("percent is %f" % 99.769273703747)
# 保留2位小数,四舍五入。# percent is 99.77
msg.append("percent is %.2f" % 99.769273703747)
# 打印百分号。 percent is 99.77 %
msg.append("percent is %.2f %%" % 99.769273703747)# 事实上,字符串格式化遵循下面的格式
# %[(name)][flag][width][.precision]type
# name是用来替换的键名
# flag有+-空格0
# flag +表示右对齐,正数前加+,负数前加-
# flag -表示左对齐,正数前不加,负数前加-
# flag 空格表示右对齐,正数前加空格,负数前加-
# flag 0表示右对齐,正数前啥都不加,负数前加-
# width表示对齐宽度
# .precision表示浮点数保留小数位
tpl = "i am %(name)-10s, age %(age)3d, percent %(pp)8.2f %%"
# i am Kevin     , age   9, percent    98.39 %
# i am Jeffson   , age 136, percent  -192.34 %
# i am Urazimil  , age  69, percent    93.10 %
msg.append(tpl % ({"name": "Kevin", "age": 9, "pp": 98.39263}))
msg.append(tpl % ({"name": "Jeffson", "age": 136, "pp": -192.3439263}))
msg.append(tpl % ({"name": "Urazimil", "age": 69, "pp": 93.1}))# 字典方式格式化,对比字符串的格式化方法,这种方式更灵活多样.
# 如果使用了%s %d 或 %f等方式格式化,那么输出百分号符号的方法就是%%
msg.append("i am %(name)s, age %(age)d, percent %(pp).2f %%."% ({"name": "Kevin", "age": 19, "pp": 97.562333477})
)# 除了以上%格式符号,还有format方法格式化
# 没有使用%s %d 或 %f等方式格式化,那么输出百分号符号就是%
tpl = "i am {name}, age {age}, percent {pp} %."
msg.append(tpl.format(name="Kevin", pp=97.562333477, age=29))
msg.append(tpl.format(**{"name": "Kevin", "age": 29, "pp": 97.562333477}))
msg.append(tpl.format_map({"name": "Kevin", "age": 29, "pp": 97.562333477}))
tpl = "i am {2}, age {0}, percent {1} %."
msg.append(tpl.format(29, 97.56233347, "Kevin"))# 还有这种方式,取后面传入的第几个列表的第几个元素。
tpl = "i am {0[0]}, age {0[1]}, percent {0[0]} %."
msg.append(tpl.format([1, 2, 3], [11, 22, 33]))
tpl = "i am {1[0]}, age {1[1]}, percent {1[0]} %."
msg.append(tpl.format([1, 2, 3], [11, 22, 33]))# 还有这种方式,*取出列表里的元素?暂时没搞懂以后搞懂。
tpl = "i am {:s}, age {:d}, percent {:f} %."
msg.append(tpl.format(*["Kevin", 29, 97.562333477]))# 再了解其他介个格式化符号
# :b for binary 二进制方式显示数字
# :o for octoanry 八进制方式显示数字
# :d for decimal 十进制方式显示数字
# :x,:X for hexadecimal 十六进制方式显示数字,x小写,X大写
# :% for percent 以百分比方式显示,取小数点后6位,自带百分号
# numbers: 1111, 17, 15, f, F, 1595.272937%
tpl = "numbers: {:b}, {:o}, {:d}, {:x}, {:X}, {:%}"
msg.append(tpl.format(*[15, 15, 15, 15, 15, 15.952729374]))for i in msg:print(i)pass# for输出结果
# i am mysql, which is a database.
# i am db2, which is a database.
# i am oracle, which is a database, too.
# these are database, ['mysql', 'db2', 'oracle']
# i am Kevin, age 29.
# i am Kevin, age 29.
# percent is 99.769273703747
# percent is 99.769274
# percent is 99.77
# percent is 99.77 %
# i am Kevin     , age   9, percent    98.39 %
# i am Jeffson   , age 136, percent  -192.34 %
# i am Urazimil  , age  69, percent    93.10 %
# i am Kevin, age 19, percent 97.56 %.
# i am Kevin, age 29, percent 97.562333477 %.
# i am Kevin, age 29, percent 97.562333477 %.
# i am Kevin, age 29, percent 97.562333477 %.
# i am Kevin, age 29, percent 97.56233347 %.
# i am 1, age 2, percent 1 %.
# i am 11, age 22, percent 11 %.
# i am Kevin, age 29, percent 97.562333 %.
# numbers: 1111, 17, 15, f, F, 1595.272937%

 


http://lihuaxi.xjx100.cn/news/236106.html

相关文章

个人项目-小学四则运算 “软件”之初版

本次作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2166 我的github远程仓库的地址:https://github.com/yanyuluu/yanyuluu/tree/master/ruanjiangc 第一部分:要求 具体要求:任何编程语言都可以&#xf…

单例测试模式中【饿汉式】与【懒汉式】的区别

package day25.thread;/** /*** author Mr Chen* create 2018-10-09 18:37* 单例测试模式:保证类在内存中只有一个对象*/ public class Dome01 {public static void main(String[] args){Singleton s1 Singleton.s; //成员变量被私有&#xf…

Python基础10-函数基础

目录 函数的定义 函数的返回值 函数的参数 参数的传递 参数的默认值 可变长参数 全局变量与局部变量 函数嵌套定义 风湿理论——函数即变量 函数的定义 定义函数的关键字def。函数名,本例函数名是test。小括号里面是参数。冒号后缩进的代码块是函数内容。…

电脑录音软件哪个好,怎么用电脑录音

如今科技迅速发展,不仅唱歌的时候喜欢录音,就连追剧看电视都喜欢把一些经典或者搞笑的音频录制下来,很多喜剧给我们的休闲时光带来了欢声笑语,碰到经典的对话或者旁白总想录制下来,那电脑录音软件哪个好,怎…

Python基础11-函数式编程与内置函数

目录 函数即变量 lambda关键字定义匿名函数 高阶函数 内置函数map 内置函数filter 内置函数reduce 内置函数看文档 函数即变量 书接上回,Python里面,函数就是变量,可以被当成普通变量一样作为返回值,调用。 def foo():pr…

排序学习之---快速排序

一、前言 快速排序是一种交换排序,它由C. A. R. Hoare在1962年提出。 二、算法思想 快速排序的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分:分割点左边都是比它小的数,右边都是比它大的数。 然后再按此方法对这两部…

vue问题

问题一:我在打包完成后,打开index.html文件发现地址并没有携带路由。去config文件夹下的index.js中寻找问题。index.js中的build命令的配置有一个属性叫assetsPublicPath,它的值为‘/’。意思是根目录,这时会从index.html所在的硬…

填报表中也可以添加 html 事件

在实际的项目开发中,填报表的应用十分广泛。 多数情况下,填报表会作为整个项目的一部分配合需求灵活使用,但有时也会受大项目环境的影响,产生一些特别的要求。比如,通常报表单元格的数据类型大多是文本,有时…