美文网首页
笨方法学Python-习题7-更多打印

笨方法学Python-习题7-更多打印

作者: Python探索之路 | 来源:发表于2020-01-13 11:50 被阅读0次

在下面这道习题中,又会看到两个新的语法点。

  1. 某段内容打印多遍,print("content" * count), content代表内容,count代表重复次数。
  2. 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_运行结果

从运行结果,不难看出:

  1. print("." * 10)打印出了10个“.”。
  2. print函数如果在结尾传入end=" "的参数,直接用空格连接了下面的打印内容,而不是默认的换行。

小结

  1. 重复打印的语法,print("content" * count), content代表内容,count代表重复次数。
  2. 打印内容如何不换行的方法。

相关文章

网友评论

      本文标题:笨方法学Python-习题7-更多打印

      本文链接:https://www.haomeiwen.com/subject/xhowactx.html