《 笨方法学 Python 》_ 习题 10 - 12

习题 10:那是什么
# 使用反斜杠(\)可以将难打印出来的字符放到字符串
tabby_cat = "\tI'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)

Python 支持的转义序列:

        转义字符        功能
        \\              反斜杠(\)
        \'              单引号(')
        \"              双引号(")
        \a              ASCII响铃符(BEL)
        \b              ASCII退格符(BS)
        \f              ASCII进纸符(FF)
        \n              ASCII换行符(LF)
        \N{name}        Unicode 数据库中的字符名,name 是它的名字,仅适用 Unicode
        \r              ASCII回车符(CR)
        \t              ASCII水平制表符(TAB)
        \uxxxx          值为 16 位十六进制值 xxxx 的字符(仅适用于 Unicode)
        \Uxxxxxxxx      值为 32 位十六进制值 xxxxxxxx 的字符(仅适用于 Unicode)
        \v              ASCII垂直制表符(VT)
        \ooo            值为八进制值 ooo 的字符
        \xhh            值为十六进制值数 hh 的字符
习题 11:提问
print("How old are you?", end='')
age = input()                       # Python 3.0 版本后用 input 替换了 raw_input
print("How tall are you?", end='')
height = input()
print("How much do you weigh?", end='')
weight = input()

print("So, you're %r old, %r tall and %r heavy." % (age, height, weight))

习题 12:提示别人
age = input("How old are you?")
height = input("How tall are you?")
weight = input("How much do you weigh?")

print("So, you're %r old, %r tall and %r heavy." % (age, height, weight))

猜你喜欢

转载自blog.csdn.net/yz19930510/article/details/80526146