python3 _笨方法学Python_日记_DAY2
Day 2
-
习题 6: 字符串(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)
print(x)
print(y)
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)
There are 10 types of people.
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.
加分题:
添加注释
# 定义x为一个字符串,其中用了整型%d的格式化字符
x = "There are %d types of people." % 10
# 定义binary和do_not 均为两个字符串
binary = "binary"
do_not = "don't"
# 格式化字符 %s 字符串,将binary和do_not字符串去掉引号,插入到新的字符串y中
y = "Those who know %s and those who %s." % (binary, do_not)
# 输出x和y
print(x)
print(y)
# 格式化字符%r 照原样打印,所以把x连带引号一起打印出来了
print("I said: %r." % x)
# 格式化字符%s 字符串,去掉了y中的引号,但是在打印的格式中,用引号把%s括起来了,所以最后还是显示有引号
print("I also said: '%s'." % y)
# 定义新变量,为布尔类型的值False
hilarious = False
# 定义字符串
joke_evaluation = "Isn't that joke so funny?! %r"
# 格式化字符%r 原样打印,布尔类型直接打印False
print(joke_evaluation % hilarious)
# 定义字符串w和e
w = "This is the left side of..."
e = "a string with a right side."
# 字符串相加就是连接起来
print(w+e)
- 习题 7: 更多打印
print("Mary had a little lamb.")
print("Its fleece was white as %s." % 'snow')
print("And everywhere that Mary went.")
print("." * 10 )#重复10次
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,
end7+end8+end9+end10+end11+end12)
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger
-
习题 8: 打印,打印
formatter = "%r %r %r %r"
print(formatter % (1,2,3,4))
print(formatter % ("one", "two", "three", "four"))
print(formatter % (True, False, False, True))
print(formatter % (formatter, formatter, formatter, formatter))
print(formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
))
# 换言之
formatter="'I had this thing.' 'That you could type up right.' 'But it didn't sing.' 'So I said goodnight.'"
print(formatter)
# 怎么不一样捏,第三个在前一种方案出现了双引号
# 原来是"But it didn't sing."这一句中已经出现了单引号,所以在格式化的时候为了避免认错,首尾换成了双引号
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
'I had this thing.' 'That you could type up right.' 'But it didn't sing.' 'So I said goodnight.'
python里使用单引号和双引号没有意思上的区别,当句中有单引号的时候会强制使用来双引号区分。
-
习题 9: 打印,打印,打印
# Here's some new strange stuff, remember type it exactly.
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print("Here are the days:",days)
print("Here are the months:",months)
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.
""")
结果:
Here are the days: Mon Tue Wed Thu Fri Sat Sun
Here are the months: Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
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.
三双引号可以放置多行文字
\n 为转义字符,换行
-
习题 10: 那是什么?
转义字符:
|
-
习题 11: 提问
1 age = input("How old are you?") 2 height = input("How tall are you?") 3 weight = input("How much do you weigh") 4 5 print("So, you are %r old, %r tall and %r heavy." % (age, height, weight))
How old are you?3
How tall are you?2
How much do you weigh1
So, you are '3' old, '2' tall and '1' heavy.
str = input("请输入:"); print ("你输入的内容是: ", str)

浙公网安备 33010602011771号