#task1
#使用字符串的format()方法,对输出数据进行格式化
x1, y1 = 1.2, 3.57
x2, y2 = 2.26, 8.7
#输出1
print('{:-^40}'.format('输出1'))
print('x1 = {} , y1 = {}'.format(x1, y1))
print('x2 = {} , y2 = {}'.format(x2, y2))
#输出2
print('{:-^40}'.format('输出2'))
print('x1 = {:.1f}, y1 = {:.1f}'.format(x1,y1))
print('x2 = {:.1f}, y2 = {:.1f}'.format(x2,y2))
#输出3
print('{:-^40}'.format('输出3'))
print('x1 = {:<15},y1 = {:<15}'.format(x1,y1))
print('x2 = {:<15},y2 = {:<15}'.format(x2,y2))
总结:print中的{:.1f} 小数点后的数字是多少,则输出数据的小数位就有多少,可以有指数形式表示后面的位数。
#task2
x=input('请输入一段英文字符:')
a=x.upper()
b=a.replace('A','')
print(b)
#task3
#把姓名的第一个字母转换成大写,遍历输出
name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
n=1
for name in name_list:
print(f'{n}:{name.title()}')
n+=1
#task4
name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
name_list.sort()
n=1
for name in name_list:
print(f'{n}.{name.title()}')
n+=1
#task5
text='''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!'''
hang=len(text.splitlines())
print('行数:',hang)
c=text.split()
print('单词数:',len(c))
a=text.replace(' ','')
print('字符数',len(a))
print('空格数:',len(c)-1)
#task6
i=3
list=[]
while i!=0:
n=input('输入想要加入愿望清单的事情:')
list.append(n)
i-=1
n = 1
print('{:-^50}'.format('我的愿望清单'))
for j in list:
print(f'{n}. {j}')
n += 1
实验总结:
一、format函数使数据格式化更加方便,而且可以在print函数中对变量进行限制或者在其左右增添字符。
二、name.title()将开头字母大写
三、注意实验要求,字符数不等于符号数,小心看题,不然可能花费大量时间做无用功。
四、创立一个空列表,可以用 list=[ ]