Python的3种格式化字符串方法
Python中有3种format字符串的方式:
- 传统C语言式
- 命名参数
- 位置参数
1. 传统C语言式
和c语言里面的 sprintf 类似,参数格式也一样
title = "world"
year = 2013
print "hello %s, %10d" % (title, year)
这种方式有一个陷阱,比如 title 是一个list,执行语句 "hello %s" % title,会触发一个异常;正确的做法是先把 title 转换为 string,或用 string.format
2. 命名参数
title = "world"
year = 2013
print "hello %(title)s, %(year)10d" % {"title":title, "year":year}
string.format 也支持命名参数:
"hello {title}, {year}".format(title=title, year=year)
"hello {title}, {year}".format(**{"title":title, "year":year})
string.format还有一个地方需要注意,如果参数是unicode,则string也必须是unicode,否则,会触发异常。如执行下述语句会出错:
title = u"标题党"
"hello {0}".format(title)
3. 位置参数
title = "world"
year = 2013
print "hello {0}, {1}".format(title, year) print "today is {0:%Y-%m-%d}".format(datetime.datetime.now())
总结
string.format 比 % 格式化方式更优雅,不容易出错
参考
- % 格式详解:http://docs.python.org/2/library/stdtypes.html#string-formatting-operations
- string.format 语法详解:http://docs.python.org/2/library/string.html#format-string-syntax
- string.format 例子:http://docs.python.org/2/library/string.html#formatexamples
Python的3种格式化字符串方法的更多相关文章
- [转]Python的3种格式化字符串方法
本文转自: 夏日微风Python笔记 传统C语言式 命名参数 位置参数 1. 传统C语言式 和c语言里面的 sprintf 类似,参数格式也一样 title = "world" y ...
- Python中用format函数格式化字符串
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. 1.百分号方式 语法:%[( ...
- Python中用format函数格式化字符串的用法
这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下 自python2. ...
- 【python】format函数格式化字符串的用法
来源:http://www.jb51.net/article/63672.htm 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式 ...
- Python的三种格式化输出
今天刚学了python的三种格式化输出,以前没接触过这么有趣的输出方式,现在来分享一下. #!/user/bin/env python#coding:utf-8#三种格式化输出 #第一种格式化输出na ...
- WPF中Binding使用StringFormat格式化字符串方法
原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...
- python 列表,元组,字符串方法和属性
python序列包含列表[].元组().字符串三种 -------列表-------------- 一.列表基本内容 1.建立:a=[1,2,3,5],通过[ , ,], >>>b= ...
- python 零散记录(三) 格式化字符串 字符串相关方法
使用 % 符号格式化字符串: """常用转换说明符:""" #%s: 按照str()方式转换 #%r: 按照repr()方式转换 #%d: ...
- C#实现类似"hello $world"的格式化字符串方法
C#自带的string.Format可以格式化字符串,但是还是不太好用,由于格式的字符占位符都是数字,当数目较多时容易混淆.其实可以扩展string的方法,让C#的字符串具备其他的方法,下面介绍一个实 ...
随机推荐
- AtCoder - 2153 An Ordinary Game list模拟 || 博弈
http://abc048.contest.atcoder.jp/tasks/arc064_b?lang=en 在vj里面用list模拟水过去了,然后感觉vj不靠谱,上atcoder交,果然tle 我 ...
- 基于CentOS 7.2个人网盘的实现
首先使用YUM安装依赖环境: [root@sishen ~]#yum install python python-setuptools python-imaging python-ldap pytho ...
- Linux上不了网——wget无法解析主机
很有可能是网关和域名服务器没有设置 1.设置网关 netstat -rn #查看网关配置情况 [hadoop@slave1 ~]$ route -n Kernel IP routing table D ...
- 多线程wait和notify实现1212
package threadT; public class ThreadMain { public static void main(String args[]) { final Object obj ...
- 11.1Java-接口
一.接口 interface定义:固定格式 public abstract 返回值类型 方法名字(参数列表);代码: public interface AMyInterface { public ab ...
- 【学习笔记】OSG中相机参数的更改
#pragma comment(lib, "osg.lib") #pragma comment(lib, "osgDB.lib") #pragma commen ...
- IOS的水滴文件效果
@implementation ViewController - (void)viewDidLoad{ [super viewDidLoad]; NSDictionary *dict = [NSDic ...
- How `new’ operator works ?
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=15 February 15, 2013 How `new’ operator ...
- vijos 1053 Easy sssp
描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 要求你写一个程序, 判断这个有向图中是否存在负权回路. 如果从一 ...
- 面向对象的设计的SOLID原则
S.O.L.I.D是面向对象设计和编程中5个重要编码规则的首字母的缩写. - SRP The Single Responsibility Principle 单一责任原则 当需要修改某个类的时候原因有 ...