实验二
作者:互联网
x = list(range(10)) print('整数输出1: ', end = '') for i in x: print(i, end=' ') print('\n整数输出2: ', end = '') for i in x: print(f'{i:02d}', end = '-') # 指定每个整数输出宽度占两列,不足两列,左边补0 print('\n整数输出3: ', end = '') for i in x[:-1]: print(f'{i:02d}', end = '-') print(f'{x[-1]:02d}') print('\n字符输出1: ', end = '') y1 = [str(i) for i in range(10)] # 函数str()用于把其它类型对象转换成字符串对象 print('-'.join(y1)) print('字符输出2: ', end = '') y2 = [str(i).zfill(2) for i in range(10)] # 方法.zfill()用于对字符串进行格式化, 指定宽度为2列,不足左边补0 print('-'.join(y2))
# 把姓名转换成大写,遍历输出 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins'] # 实现方式1 for name in name_list: print(name.title()) print() # 实现方式2 name_list_captilize = [name.title() for name in name_list] print('\n'.join(name_list_captilize))
name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins'] num=[1,2,3,4,5] name_list1 = [name.title() for name in name_list] i=1 for i in range(5): print(num[i],'.',name_list1[i],end='') print()
import this 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!''' line = text.count('\n')+1 print(f'行数:{line}') x=text.split(' ')
word = len(x) print(f'单词数:{word}') count = len(text) print(f'字符数:{count}') space = text.count(' ') print(f'空格数:{space}')
标签:end,name,list,better,实验,print,than 来源: https://www.cnblogs.com/miaoqiuyousi/p/16139146.html