python中更多种类的打印

这是学习python的第7课,我学习到了python中更多种类的打印方法。

1.print("." * 10) 是输出10个 "." , 把每个字母相加输出就可以得到相应的字符串。

print("Mary had a little lamb.")
print("Its fleece was white as {}." .format('snow'))   #将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 = ' ' )  # end = ' ' 为连接前后的成分,使其不换行
print(end7 + end8 + end9 + end10 + end11 + end12)

运行结果:

 

2.用了一个自定义的函数 formatter,其作用是:

<1>.取第1行定义的 formatter 字符串。

<2>.调用它的 format 函数,这相当于告诉它执行一个叫 format 的命令行命令。

<3>.给 format 传递4个参数,这些参数和 formatter 变量中的{}匹配,相当于将参数传递给了 format 这个命令。

<4>.在 formatter 上调用 format的结果是一个新字符串,其中的{}被4个变量替换掉了,这就是 print 现在打印出的结果。

formatter="{} {} {} {}"

print(formatter.format(1,2,3,4))
print(formatter.format("one","two","three","four"))
print(formatter.format(True,False,False,True))
print(formatter.format(formatter,formatter,formatter,formatter))
print(formatter.format(
    "Try your",
    "Own text here",
    "Maybe a poem",
    "or a song about fear"       
))

 运行结果:

 

3.这个主要是讲定义的字符串可以直接打印出来,“\n” 是换行符。

# Here's some new strange stuff, remember type it exactlyself.

days = "Mon Tue Wed Thu Fri Sat Sun"
months = "\nJan\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.
   """)

运行结果: 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风自在 流水潺潺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值