栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python中更多种类的打印

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 = "nJannFebnMarnAprnMaynJunnJulnAug"

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.
   """)
运行结果:
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/833751.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号