首页 > 其他 > 详细

4.2

时间:2021-04-02 21:57:12      阅读:28      评论:0      收藏:0      [点我收藏+]


# 使用字符串的format()方法,对输出数据项进行格式化
x1, y1 = 1.2, 3.57
x2, y2 = 2.26, 8.7
# 输出1
print(‘{:-^40}‘.format(‘输出1‘)) # {:-^40}控制输出数据格式: 宽度占40列,居中对齐,空白处用-补齐
print(‘x1 = {}, y1 = {}‘.format(x1, y1))
print(‘x2 = {}, y2 = {}‘.format(x2, y2))
# 输出2
print(‘{:-^40}‘.format(‘输出2‘)) # {:-^40}控制输出数据格式: 宽度占40列,居中对齐,空白处用-补齐
print(‘x1 = {:.1f}, y1 = {:.1f}‘.format(x1, y1)) # {:.1f}控制小数输出精度,保留1位小数
print(‘x2 = {:.1f}, y2 = {:.1f}‘.format(x2, y2))
# 输出3
print(‘{:-^40}‘.format(‘输出3‘)) # {:-^40} 控制输出数据格式: 宽度占40列,居中对齐,空白处用-补齐
print(‘x1 = {:<15}, y1 = {:<15}‘.format(x1, y1)) # {:<15}控制数据输出宽度占15列,左对齐,空白处默认补空格
print(‘x2 = {:<15}, y2 = {:<15}‘.format(x2, y2))

技术分享图片

 

 

 

 

二.

n = input("请输入字符串:")
n1 = n.replace("A","")
n2 = n1.upper()
print(n2)

技术分享图片

 

 

 

 

 

 

 


三.

 

 

 

namelist = ["david bowie","louis armstrong","leonard cohen","bob dylan","cocteau twins"]
n = 1
for name in namelist:
print(f‘{n}.{name.title()}‘)
n +=1

技术分享图片

 

 

四.

namelist = ["david bowie","louis armstrong","leonard cohen","bob dylan","cocteau twins"]
n = 1
for name in sorted(namelist):
print(f"{n}.{name.title()}")
n += 1


技术分享图片

 

 

五.

T = """The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren‘t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you‘re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it‘s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let‘s do more of those!"""
a = len(T.splitlines())
b = len(T.split())
c = len(T)
d = T.count("")
print(f‘行数:{a}单词数:{b}字符数:{c}空格数:{d}‘)

技术分享图片

 

 

 

 

六.

n = 1
list = []
for n in range(3):
X = input("输入你的心愿:")
Xlist = list.append(X)
n += 1
print(‘{:-^50}‘.format(‘我的心愿单‘))
for n in range(1,4):
print(n ,end="")
print("."+list[n-1])

 

技术分享图片

 

 技术分享图片

 

4.2

原文:https://www.cnblogs.com/glz12138/p/14611796.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!