重新梳理Python基础(2)

1. String类型输出,看示例

x = "There are %d types of people" % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s" % (binary, do_not)
z = "Those who know %r and those who %r" % (binary, do_not)

print x
print y
print z

print "I said: %r." % x
print "I also said: '%s'" % y

hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"

print joke_evaluation % hilarious

w = "This is the left side of..."
e = "a string with a right side."

print w + e 

值得注意的是%r显示一个字符串是带单引号的。

%r是用来debuging的,输出原始数据,就是一个变量是什么就输出什么

以下是输出

There are 10 types of people
Those who know binary and those who don't
Those who know 'binary' and those who "don't"
I said: 'There are 10 types of people'.
I also said: 'Those who know binary and those who don't'
Isn't that joke so funny?! False
This is the left side of...a string with a right side.

2. print的时候,当这行末尾有逗号时,不会换行,例如

end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

print end1 + end2 + end3 + end4 + end5 + end6, #如果没逗号,会输出两行,如果有逗号,会输出在同一行
print end7 + end8 + end9 + end10 + end11 + end12

输出

Cheese Burger

 3. Escape Sequences

View Code
tabby_cat = "\tI am tabbed in"
persian_cat = "I am split \non a line."
backslash_cat = "I am \\ 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
while True:
    for i in ["/","-","|","\\","|"]:
        print "%s\r" % i,    #\r表示退格

输出结果

View Code
        I am tabbed in
I am split
on a line.
I am \ a \ cat

I'll do a list:
        * Cat food
        * Fishies
        * Catnip
        * Grass
原文地址:https://www.cnblogs.com/dollarzhaole/p/2959572.html