需要关注print
在print语句的最后加","可以避免换行
e.g. 1. print "Let's talk about %s." % my_name
2. formatter = "%r %r %r %r"
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)
3. print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""
10.14补充
还可以这样用:
print """
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer)
10.22补充
在最后加上一个,
可以避免自动输出/n
e.g. print current_file.readline(),
10.14补充
还可以这样用:
print """
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer)
10.22补充
在最后加上一个,
可以避免自动输出/n
e.g. print current_file.readline(),
顺便把Keywords, Escape Sequences, Formatters整理出来
>>> keyword.kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while',
'with', 'yield']
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while',
'with', 'yield']
转义符
|
功能
|
\\
|
Backslash () 反斜杠
|
\'
|
Single quote (‘) 单引号
|
\"
|
Double quote (”) 双引号
|
\a
|
ASCII Bell (BEL) 响铃符
|
\b
|
ASCII Backspace (BS) 退格符
|
\f
|
ASCII Formfeed (FF) 进纸符
|
\n
|
ASCII Linefeed (LF) 换行符
|
\N{name}
|
Unicode 数据库中的字符名,其中 name 就是它的名字(Unicode only)
|
\r ASCII
|
Carriage Return (CR) 回车符
|
\t ASCII
|
Horizontal Tab (TAB) 水平制表符
|
\uxxxx
|
值为 16 位十六进制值xxxx 的字符(Unicode only)
|
\Uxxxxxxxx
|
值为 32 位十六进制值 xxxx 的字符(Unicode only)
|
\v
|
ASCII Vertical Tab (VT) 垂直制表符
|
\ooo
|
值为八进制值 ooo 的字符
|
\xhh
|
值为十六进制数 hh 的字符
|
| 格式化字符串 | 说 明 |
| %c | 格式化字符及其ASCII码 |
| %s | 格式化字符串 |
| %d | 格式化整数 |
| %u | 格式化无符号整型 |
| %o | 格式化无符号八进制数 |
| %x | 格式化无符号十六进制数 |
| %X | 格式化无符号十六进制数(大写) |
| %f | 格式化浮点数字,可指定小数点后的精度 |
| %e | 用科学计数法格式化浮点数 |
| %E | 作用同%e,用科学计数法格式化浮点数 |
| %g | 根据值的大小决定使用%f活%e |
| %G | 作用同%g,根据值的大小决定使用%f活%e |
| %p | 用十六进制数格式化变量的地址 |
没有评论:
发表评论