python 字符串格式化—format
Python2.6 开始,新增了一种格式化字符串的函数 str.format()。使用起来简单方便,不会遇到使用%时候格式的选择问题。
按照参数默认顺序
>>> "yesday is {}, today is {}".format("saturday", "sunday")
'yesday is saturday, today is sunday'
>>>
指定参数顺序
>>> "yesday is {0}, today is {1}, good day is {0}".format("saturday", "sunday")
'yesday is saturday, today is sunday, good day is saturday'
>>>
指定参数名称
#!/usr/bin/python
s = "I am a {name}, and study {subject}".format(name="coder", subject="ES")
print s
output:
I am a coder, and study ES
使用字典做参数
# dict
d = {"name": "coder", "subject": "VUE"}
s = "I am a {name}, and study {subject}".format(**d)
print s
output:
I am a coder, and study VUE
使用列表做参数
# list
l = ["coder", "big data"]
s = "I am a {0}, and study {1}".format(*l)
print s
output:
I am a coder, and study big data
使用对象做参数
class Orange(object):
def __init__(self, v):
self.value = v
orge = Orange(5)
s = "The PH of an orange is {.value}".format(orge)
print (s)
output:
The PH of an orange is 5
python 字符串格式化—format的更多相关文章
- python 字符串格式化format
通过{}和:来代替传统%方式 1.位置参数 位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 >>> li ...
- python 字符串格式化 format
用法: 它通过{}和:来代替传统%方式 1.使用位置参数 要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 ...
- Python字符串格式化--format()方法
https://blog.csdn.net/i_chaoren/article/details/77922939 csdn
- Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助
Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助 目录 Pychar ...
- Python 字符串格式化
Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...
- python字符串格式化 %操作符 {}操作符---总结
Python字符串格式化 (%占位操作符) 在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出.Python中内置有对字符串进行格式化的操作 %. 模板 格式化字 ...
- 【转】Python字符串格式化
Python 支持格式化字符串的输出 .尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中. 在 Python 中,字符串格式化使用与 C 中 sp ...
- Python中格式化format()方法详解
Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任何东西都是对象; 字符串的参 ...
- 7. python 字符串格式化方法(2)
7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...
随机推荐
- python生产者消费者模型优点
生产者消费者模型:解耦,通过队列降低耦合,支持并发,生产者和消费者是两个独立的并发体,他们之间使用缓存区作为桥梁连接,生产者指望里丢数据,就可以生产下一个数据了,消费者从中拿数据,这样就不会阻塞,影响 ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph(暴力搜索)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6152 Problem Description It is well known that small ...
- POJ 2369 Permutations(置换群概念题)
Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...
- 设计精美Power BI报告的五大秘诀
众所周知,Power BI可以帮助您创建交互式且信息丰富的报告,但使用Power BI 制作精美而实用的报告对我们这群IT人员而言,却是一个巨大的痛苦:但个人觉得不能就此止步,通过不断实践练习,小悦采 ...
- lintcode 刷题 by python 部分链表题总结(2)
本篇博客对最近做的链表的算法题做个简单的小结,主要描述题目和提供解题思路,具体代码见我的 github:https://github.com/MUSK1881/lintcode-by-python 3 ...
- 11.2.0.4rac service_name参数修改
环境介绍 )客户环境11. 两节点 rac,集群重启后,集群资源一切正常,应用cs架构,连接数据库报错,提示连接对象不存在 )分析报错原因,连接数据库方式:ip:Port/service_name方式 ...
- 【opencv基础】linux系统卸载opencv
找到opencv某个版本的源码文件,进入build目录: cd opencv_build sudo make uninstall cd .. sudo rm -r build sudo rm -r / ...
- [LeetCode&Python] Problem 746. Min Cost Climbing Stairs
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- [Algorithm] Good Fibonacci
def good_fibonacci(n): if n<=1: return (n,0) else: (a,b)=good_fibonacci(n-1) return (a+b,a)
- Unity反射探针用法教程
Unity 3D反射探针 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心 ...