>>> import datetime
>>> name = 'Fred'
>>> age = 50
>>> anniversary = datetime.date(1991, 10, 12)
>>> f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
'My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.'
>>> f'He said his name is {name!r}.'
"He said his name is 'Fred'."
其它语言里面也有,现在Python终于也要加上了,用起来还是很方便的
1
freefcw 2015-08-09 10:47:50 +08:00
这个看着不错,很简洁
|
2
janxin 2015-08-09 10:49:42 +08:00
需要3.6,估计要明年才能用上了吧
|
3
Septembers 2015-08-09 10:53:46 +08:00 via Android
@janxin 3.5才发布没多久
|
4
janxin 2015-08-09 10:58:59 +08:00
@Septembers 3.5都还没rc,大概3.6要明年了...
|
5
jjx 2015-08-09 14:04:24 +08:00
|
6
banxi1988 2015-08-09 15:41:20 +08:00
这语法 ,通过一个Python 特有的前缀 表示法,好像可以少写一个转义字符。
比如 Ruby 是 "#{}" ,Swift 是"\()" |
7
kikyous 2015-08-09 17:54:13 +08:00
ruby和coffee都用n年了
|
8
cute 2015-08-10 14:01:41 +08:00
楼上这么说,php只能呵呵了。
|
9
zonyitoo 2015-08-10 14:42:13 +08:00
String Interpolation, 脚本语言实现最方便了
|
10
Jex 2015-08-11 12:05:14 +08:00 1
语法有点不一致, Interpolation 直接 Expression 就行了,{name!r} 和 {anniversary:%A, %B %d, %Y} 这种增加了不必要的特殊语法
|
11
justahappy 2015-09-20 20:58:13 +08:00
@cute 专科生用的语言 哪来的优越感????????
|