在下面这道习题中,又会看到两个新的语法点。
- 某段内容打印多遍,print("content" * count), content代表内容,count代表重复次数。
- print函数打印不换行。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
print("Mary had a little lamb.")
print("Its fleece was white as {}.".format('snow'))
print("And everywhere that Mary went.")
print("." * 10) # what'd that do?
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
# watch that comma at the end. try removing it to see what happens
print(end1 + end2 + end3 + end4 + end5 + end6, end=' ')
print(end7 + end8 + end9 + end10 + end11 + end12)
运行结果如下:
ex7_运行结果从运行结果,不难看出:
- print("." * 10)打印出了10个“.”。
- print函数如果在结尾传入end=" "的参数,直接用空格连接了下面的打印内容,而不是默认的换行。
小结
- 重复打印的语法,print("content" * count), content代表内容,count代表重复次数。
- 打印内容如何不换行的方法。
网友评论