首页 > 其他分享 >实验2 字符串和列表

实验2 字符串和列表

时间:2023-03-23 13:56:42浏览次数:46  
标签:10 list len 列表 better print while 实验 字符串

实验任务1

task1

 1 x = 'nba FIFA'
 2 print(x.upper())
 3 print(x.lower())
 4 print(x.swapcase())
 5 print()
 6 
 7 x = 'abc'
 8 print(x.center(10, '*'))
 9 print(x.ljust(10, '*'))
10 print(x.rjust(10, '*'))
11 print()
12 
13 x = '123'
14 print(x.zfill(10))
15 x = 123
16 print(str(x).zfill(10))
17 print()
18 
19 x = 'phone_number'
20 print(x.isidentifier())
21 x = '222test'
22 print(x.isidentifier())
23 print()
24 
25 x = ' '
26 print(x.isspace())
27 x = '\n'
28 print(x.isspace())
29 print()
30 
31 x = 'python is fun'
32 table = x.maketrans('thon', '1234')
33 print(x.translate(table))
View Code

 

 实验任务2

task2

 1 x = [5, 11, 9, 7, 42]
 2 print('整数输出1:', end= '')
 3 i = 0
 4 while i < len(x):
 5     print(x[i], end= ' ')
 6     i += 1
 7 
 8 print('\n整数输出2: ', end= '')
 9 i = 0
10 while i < len(x):
11     print(f'{x[i]:02d}', end= '-')
12     i += 1
13 
14 print('\n整数输出3: ', end= '')
15 i = 0
16 while i < len(x) - 1:
17     print(f'{x[i]:02d}', end= '-')
18     i += 1
19 print(f'{x[-1]:02d}')
20 
21 print('\n字符输出1: ', end= '')
22 y1 = []
23 i = 0
24 while i < len(x):
25     y1.append(str(x[i]))
26     i += 1
27 print('-'.join(y1))
28 
29 print('字符输出2: ', end = '')
30 y2 = []
31 i = 0
32 while i < len(x):
33     y2.append( str(x[i]).zfill(2) )
34     i += 1
35 print('-'.join(y2))
View Code

       

当 x = [1, 9, 8, 4, 2, 0, 49]

 

 

 实验任务3

task3

 1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins']
 2 i = 0
 3 while i < len(name_list):
 4     print(name_list[i].title())
 5     i += 1
 6 print()
 7 
 8 t = []
 9 i = 0
10 while i < len(name_list):
11     t.append(name_list[i].title())
12     i += 1
13 print('\n'.join(t))
View Code

 

 实验任务4

task4

1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins']
2 t = []
3 i = 0
4 while i < len(name_list):
5     t.append(name_list[i].title())
6     i += 1
7 t.sort()
8 print('\n'.join(t))
View Code

 

实验任务5

task5

 1 x = '''The Zen of Python by Tim Peters
 2 
 3 Beautiful is better than ugly.
 4 Explicit is better than implicit.
 5 Simple is better than complex.
 6 Complex is better than complicated.
 7 Flat is better than nested.
 8 Sparse is better than dense.
 9 Readability counts.
10 Special cases aren't special enough to break the rules.
11 Although practicality beats purity.
12 Errors should never pass silently.
13 Unless explicitly silenced.
14 In the face of ambiguity, refuse the temptation to guess.
15 There should be one-- and preferably only one --obvious way to do it.
16 Although that way may not be obvious at first unless you're Dutch.
17 Now is better than never.
18 Although never is often better than *right* now.
19 If the implementation is hard to explain, it's a bad idea.
20 If the implementation is easy to explain, it may be a good idea.
21 Namespaces are one honking great idea -- let's do more of those!
22 '''
23 print('行数:', len(x.splitlines()))
24 print('单词数:', len(x.split()))
25 print('字符数:', len(x.capitalize()))
26 print('空格数:', x.count(' '))
View Code

 

 实验任务6

task6

book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'],
             ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'],
             ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'],
             ['来自民间的叛逆', '袁越', '','新星出版社'],
             ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'],
             ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'],
             ['小行星掉在下午','沈大成', '', '广西师范大学出版社']]
x = '图书信息'
print(x.center(40, '*'))
i = 0
while i < len(book_list):
    print(book_list[i])
    i += 1

print()
View Code

 

标签:10,list,len,列表,better,print,while,实验,字符串
From: https://www.cnblogs.com/202280060019x/p/17242872.html

相关文章

  • 实验2
    task1源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<math.h>#defineN5#defineR1663#defineR2701intmain(){intnumber,i......
  • 实验2
    #include"stdafx.h"#include<stdlib.h>#include<stdio.h>int_tmain(intargc,_TCHAR*argv[]){inta1,a2,a3;charc1,c2,c3;doublex,y;scanf("......
  • Java开发:list列表元素遍历删除
    一、常见误区1、提前结束遍历(直接使用列表长度进行遍历)for(inti=0;i<list.size();i++){list.remove(i);}在list不断地删除元素的同时,总列表list的长......
  • 触发器插入多行数据,字符串逗号分隔
    CREATEDEFINER=yfy_cloud@%TRIGGERubulAFTERINSERTON无标题`FOREACHROWBEGINDELETEFROMbase_userrelationWHEREF_UserId=new.F_Id;INSERTINTObase_userr......
  • C# 向SqlCommand添加参数列表
    在我们写SQL时,经常会遇到IN、NOTIN这样的查询条件,这时后面的条件需要一个参数列表。我们期待可以根据数据列表[1,3,5],动态生成多个参数,即@Parameter1=1,@Parameter2=3,@Pa......
  • LeetCode344. 反转字符串
    题目描述:编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组s的形式给出。不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用O(1)的额外......
  • 实验二
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){ intnumber; inti; srand(time(0)); f......
  • 实验二
    实验任务1task1.c源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;int......
  • 实验二
    #task1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;sran......
  • 实验2
    任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;sr......