实验2

1. 实验任务1
#task1.py

#使用字符串的format()方法,对输出数据进行格式化

x1,y1=1.2, 3.57

x2,y2 = 2.26, 8.7

 

print("{:-^40}".format("输出1"))

print("x1={},y1={}".format(x1,y1))

print("x2={},y2={}".format(x2,y2))

 

print("x1={:-^40}".format("输出2"))

print("x1={:.1f},y1={:.1f}".format(x1,y1))

print("x2={:.1f},y2={:.1f}".format(x2,y2))

 

print("{:-^40}".format("输出3"))

print("x1={:<15},y1={:<15}".format(x1,y1))

print("x2={:<15},y2={:<15}".format(x2,y2)) 

实验2


 2.实验任务2

#task2.py

x = input('Enter the string:')
s = x.replace('a',' ')
 
print(s.upper())

实验2

 

3. 实验任务3

#task3.py

name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','coceau twins']

n=1
for name in name_list:
    print(f'{n}:{name.title()}')
    n += 1

实验2

4. 实验任务4

name_list=['david bowie','louis armstrong','leonard cohen','bob dylan','coceau twins']
name_list.sort()
n=1
for name in name_list:
    print(f'{n}.{name.title()}')
    n+=1

实验2


5. 实验任务5

#task5.py

test = '''
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!
'''

print(len(text.splitlines()))
print(len(text.split()))
print(len(text))
print(text.count(' '))

实验2

 

 



6. 实验任务6

#task6.py

dream = []

a = 1
while a < 4:
    x = input('请输入想要加入愿望清单的事情: ')
    a += 1
    dream.append(x)

print('{:-^50}'. format('我的愿望清单'))

n = 1
for i in dream:
    print(f'{n}. {i}')
    n += 1

实验2

 

#实验总结

1.本次实验使我对循环,列表遍历,大小写转换等知识点深入理解,学会运用。

2.我还需要对一些函数进行基础的理解,否则会导致写代码的时候忘了它们本来的用法,不至于每次都去查询资料。

3.代码是多样的,有很多的知识需要我进一步探索,希望下次能够很好的完成。

上一篇:实验2


下一篇:苹果Mac强大的批量重命名工具:A Better Finder Rename