print "I am 6'2\" tall." \" to mean" in case mistake with ending
print 'I am 6\'2" tall.' tabby_cat="\t I'm tabbed in." persian_cat="I'm split\non a line." backslash_cat="I'm \\ a \\ cat." fat_cat = """ I'll do a list: \t* Cat food \t* Fishies \t* Catnip\n\t* Grass """ print tabby_cat print persian_cat print backslash_cat print fat_cat
Here's a tiny piece of fun code to try out:
while True: for i in ["/","-","|","\\","|"]: print "%s\r" % i,
解释:\r表示回到行首 所以循环输出 | // | - / 上面倒着顺序?????
转义字符和格式化字符串放一起创建更加复杂的格式。persian_cat = "I'm spliton\v %s line." % "a"还记得%r吗?%r搭配单引号和双引号转义字符,并且打印出来。比较%r和%s,%r打印你要写到文件里的样子,%s打印你想看到的样子。persian_cat = "I\'m spliton \"%s\" %r." % ("a", "line")输出:I'm spliton "a" 'line'.注意line有个单引号,表示你要写入程序的是个字符串。
转义字符
\(在行尾) 续行,表示不换行\\ 反斜杠\' 单引号\" 双引号\a 响铃\b 退格,表示删除前面一个字符\t 水平制表符\v 垂直制表符,表示换行,然后从\v的地方开始输出。\n 换行\f 换页\r 回车 回到行首
Escape | What it does. |
---|---|
\\ | Backslash (\) |
\' | Single-quote (') |
\" | Double-quote (") |
\a | ASCII bell (BEL) |
\b | ASCII backspace (BS) |
\f | ASCII formfeed (FF) |
\n | ASCII linefeed (LF) |
\N{name} | Character named name in the Unicode database (Unicode only) |
\r | Carriage Return (CR) |
\t | Horizontal Tab (TAB) |
\uxxxx | Character with 16-bit hex value xxxx (u'' string only) |
\Uxxxxxxxx | Character with 32-bit hex value xxxxxxxx (u'' string only) |
\v | ASCII vertical tab (VT) |
\ooo |
Character
with octal value ooo
|
\xhh | Character with hex value hh |