Python格式化字符串知多少
formatStr = "Hello %s. Today is %s, Are there any activities today?"
# 初始化字符串格式化参数值,此处必须使用元组,不能使用列表
values = ('Mike', 'Wednesday')
# 格式化字符串
print(formatStr % values)
from string import Template
template = Template("$s $s $s ")
template.substitute(s = "Hello")
# 引用string模块中的Template类
from string import Template
template1 = Template("$s是我最喜欢的编程语言, $s非常容易学习,而且功能强大")
# 指定格式化参数s的值是Python
print(template1.substitute(s='Python'))
# 当格式化参数是一个字符串的一部分时,为了和字符串的其他部分区分开,
# 需要用一对大括号将格式化参数变量括起来
template2 = Template("${s}stitute")
print(template2.substitute(s='sub')) template3 = Template("$dollar$$相当于多少$pounds")
# 替换两个格式化参数变量
print(template3.substitute(dollar=20,pounds='英磅')) template4 = Template("$dollar$$相当于多少$pounds")
data = {}
data['dollar'] = 100
data['pounds'] = '英磅'
# 使用字典指定格式化参数值
print(template4.substitute(data))

print("{} {} {}".format(1,2,3)) # 运行结果:1 2 3
print("{a} {b} {c}".format(a = 1,c = 2,b = 3)) # 运行结果:1 3 2
print("{first!s} {first!r} {first!a}".format(first = "中"))
# 运行结果:原样输出:中 调用repr函数:'中' 输出Unicode编码:'\u4e2d'
print("原样输出:{first!s} 调用repr函数:{first!r} 输出Unicode编码:{first!a}".format(first = "中"))
# 将21按浮点数输出,运行结果:整数:21 浮点数:21.000000
print("整数:{num} 浮点数:{num:f}".format(num = 21))
# 将56按十进制、二进制、八进制和十六进制格式输出
# 运行结果:十进制:56 二进制:111000 八进制:70 十六进制:38
print("十进制:{num} 二进制:{num:b} 八进制:{num:o} 十六进制:{num:x}".format(num = 56))
# 将533按科学计数法格式输出,运行结果:科学计数法:5.330000e+02
print("科学计数法:{num:e}".format(num = 533))
# 将0.56按百分比格式输出,运行结果:百分比:56.000000%
print("百分比:{num:%}".format(num = 0.56))

# 运行结果:第04章
print("第{chapter:02.0f}章".format(chapter = 4));
# 让1、2、3分别以左对齐、中对齐和右对齐方式显示
print('{:<10.2f}\n{:^10.2f}\n{:>10.2f}'.format(1,2,3))
# “井号”在宽度为20的区域内中心对齐,并左右两侧添加若干个井号(#),两侧各添加8个井号
# 运行结果:######## 井号 ########
print("{:#^20}".format(" 井号 "))
# 在5.43和负号(-)之间显示“^”,运行结果:-^^^^^5.43
print("{0:^=10.2f}".format(-5.43))
# 运行结果:101011
print("{:b}".format(43))
name = 'Bill'
age = 30 print(f'姓名:{name}, 年龄:{age}')
Python格式化字符串知多少的更多相关文章
- Python格式化字符串~转
Python格式化字符串 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作 ...
- Python格式化字符串和转义字符
地址:http://blog.chinaunix.net/uid-20794157-id-3038417.html Python格式化字符串的替代符以及含义 符 号 说 明 ...
- Python格式化字符串
在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作符,非常类似C语言里的pr ...
- Python格式化字符串--format
format格式化字符串方法相较于老版%格式方法的优点: 1.不需要理会数据类型的问题,在%方法中'%s'只能替代字符串类型. 2.单个参数可以多次输出,参数顺序可以不相同. 3.填充方式十分灵活,对 ...
- Python - 格式化字符串的用法
0. 摘要 Python支持多种格式化字符串的方法,包括%-fromatting.str.format().f-strings三种,f-strings是Python3.6以后出现的一种新方法,相比其他 ...
- 【转】Python格式化字符串str.format()
原文地址:http://blog.xiayf.cn/2013/01/26/python-string-format/ 每次使用Python的格式字符串(string formatter),2.7及以上 ...
- 18.Python格式化字符串(格式化输出)
Python 提供了“%”对各种类型的数据进行格式化输出,例如如下代码: price = 108 print ("the book's price is %s" % price) ...
- Python格式化字符串(格式化输出)
熟悉C语言 printf() 函数的读者能够轻而易举学会 Python print() 函数,它们是非常类似的. print() 函数使用以%开头的转换说明符对各种类型的数据进行格式化输出,具体请看下 ...
- Python格式化字符串(f,F,format,%)
# 格式化字符串: 在字符串前加上 f 或者 F 使用 {变量名} 的形式来使用变量名的值 year = 2020 event = 'Referendum' value = f'Results of ...
随机推荐
- Python开发坦克大战
Python不仅能开发网站,爬虫数据分析等,他其实也可以写游戏,接下来就给大家分享下坦克大战的代码: PS:很多人在学习Python的过程中,往往因为遇问题解决不了或者没好的教程从而导致自己放弃,为此 ...
- CallContext类
CallContext类 转载weixin_30723433 最后发布于2019-07-20 10:42:24 阅读数 133 收藏 展开 System.Runtime.Remoting.Messa ...
- maven scope 的作用
一: 1.Maven中的依赖作用范围概述 Maven中使用 scope 来指定当前包的依赖范围和依赖的传递性.常见的可选值有:compile, provided, runtime, test, sys ...
- 【Python】成绩等级判断
score=eval(input("请输入成绩:\n")) if score>=60: grade="D" elif score>=70: grad ...
- [NOI2010] 超级钢琴 - 贪心,堆,ST表
这也算是第K大问题的套路题了(虽然我一开始还想了个假算法),大体想法就是先弄出最优的一批解,然后每次从中提出一个最优解并转移到一个次优解,用优先队列维护这个过程即可. 类似的问题很多,放在序列上的,放 ...
- socks5代理服务器搭建
1.首先,编译安装SS5需要先安装一些依赖组件 yum -y install gcc gcc-c++ automake make pam-devel openldap-devel cyrus-sasl ...
- (转)Hadoop 简介
转自:http://www.open-open.com/lib/view/open1385685943484.html mapreduce是一种模式,一种什么模式呢?一种云计算的核心计算模式,一种分布 ...
- 深度学习之numpy.poly1d()函数
1.np.poly1d()此函数有两个参数: 参数1:为一个数组,若没有参数2,则生成一个多项式,例如: p = np.poly1d([2,3,5,7]) print(p) ==>> ...
- 微信小程序图片设置圆角进入页面闪动
transform变形 当我们通过某些行为触发页面进行大面积绘制的时候,浏览器由于没有事先准备,应付渲染够呛,于是掉帧,于是卡顿.而will-change则是真正的行为触发之前告诉浏览器:“我待会儿就 ...
- mysql(4):性能分析和性能优化
性能分析 慢查询日志分析 ①查询慢查询日志的状态 show global variables like '%slow_query_log%'; ②开启慢查询日志(当mysql重启时会重置) set g ...