1. 输入一个字符串,打印所有奇数位上的字符(下标是1,3,5,7…位上的字符) 例如: 输入**’abcd1234 ‘ ** 输出**’bd24’**
1 2 3 4 5 6 str1 = 'abcd1234 ' new_str1 = '' for index in range (len (str1)): if index % 2 : new_str1+=str1[index] print (new_str1)
2. 输入用户名,判断用户名是否合法(用户名长度6~10位) 1 2 3 4 5 a = input ("请输入用户名:" ) if 6 <= len (a) <=10 : print ("用户名合法" ) else : print ("用户名不合法" )
3. 输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成) 例如: ‘abc’ — 合法 ‘123’ — 合法 ‘abc123a’ — 合法
1 2 3 4 5 6 7 8 9 a = input ("请输入用户名:" ) for i in a: if '0' <= i <= '9' or 'a' <= i <= 'z' : continue else : print ('不合法' ) break else : print ('合法' )
4. 输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母) 例如: ‘abc’ — 不合法 ‘123’ — 不合法 ‘abc123’ — 不合法 ‘Abc123ahs’ — 合法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 a = input ("请输入用户名:" ) flag = False if 'A' <= a[0 ] <= 'Z' : flag = True else : print ('用户名不合法' ) nums = 0 if flag: for i in a: if 'a' <= i <= 'z' or 'A' <= i <= 'Z' or '0' <= i <= '9' : if '0' <= i <= '9' : nums+=1 continue else : print ('用户名不合法' ) break else : if nums >=1 : print ('用户名合法' ) else : print ('用户名不合法' )
5. 输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串 例如:输入**’abc1shj23kls99+2kkk’** 输出:**’123992’**
1 2 3 4 5 6 str1 = 'abc1shj23kls99+2kkk' new_str1 = '' for i in str1: if '0' <= i <= '9' : new_str1+=i print (new_str1)
6. 输入一个字符串,将字符串中所有的小写字母变成对应的大写字母输出 (用upper方法和自己写算法两种方式实现) 例如: 输入**’a2h2klm12+’ ** 输出 ‘A2H2KLM12+’
方法1 1 2 3 4 5 6 7 8 str1 = 'a2h2klm12+' new_str1 = '' for i in str1: if 'a' <= i <= 'z' : new_str1+=chr (ord (i)-32 ) else : new_str1+=i print (new_str1)
方法2 1 2 3 4 5 6 7 8 str1 = 'a2h2klm12+' new_str1 = '' for i in str1: if 'a' <= i <= 'z' : new_str1 += i.upper() else : new_str1+=i print (new_str1)
7. 输入一个小于1000的数字,产生对应的学号 例如: 输入**’23’,输出 ‘py1901023’** 输入**’9’, 输出 ‘py1901009’** 输入**’123’,输出 ‘py1901123’**
1 2 3 4 5 str1 = 'py1901' a = input ("请输入一个小于1000的数字:" ) result = str1.ljust(9 -len (a),'0' ) result += a print ("对应的学号为:" ,result)
8. 输入一个字符串,统计字符串中非数字字母的字符的个数 例如: 输入**’anc2+93-sj胡说’** 输出:4 输入**’===’** 输出:3
1 2 3 4 5 6 7 8 str1 = input ("请输入:" ) count = 0 for i in str1: if 'a' <= i <= 'z' or 'A' <= i <= 'Z' or '0' <= i <= '9' : continue else : count += 1 print ("非数字字母的个数为:" ,count)
9. 输入字符串,将字符串的开头和结尾变成’+’,产生一个新的字符串 例如: 输入字符串**’abc123’, 输出 ‘+bc12+’**
1 2 3 str1 = input ("请输入一个字符串:" ) new_str1 = (str1[1 :len (str1)-1 ]).center(len (str1),'+' ) print ("新的字符串为:" ,new_str1)
10. 输入字符串,获取字符串的中间字符 例如: 输入**’abc1234’** 输出:‘1’ 输入**’abc123’** 输出**’c1’**
1 2 3 4 5 a = input ("请输入字符串:" ) if len (a) % 2 : print (a[len (a)//2 ]) else : print (a[len (a)//2 -1 ],a[len (a)//2 ],sep='' )
11. 写程序实现字符串函数find/index的功能(获取字符串1中字符串2第一次出现的位置) 例如: 字符串1为:how are you? Im fine, Thank you! , 字符串2为:you , 打印8
1 2 3 4 5 6 7 8 9 10 11 str1 = input ("请输入字符串1:" ) str2 = input ("请输入字符串2:" ) count = 0 if str2 in str1: for item in str1: count+=1 if item == str2[0 ]: break else : print ("字符串2不在字符串1中" ) print ("字符串1中字符串2第一次出现的位置为:" ,count-1 )
12. 获取两个字符串中公共的字符 例如: 字符串1为:abc123 , 字符串2为: huak3 , 打印:公共字符有:a3
1 2 3 4 5 6 7 8 str1 = 'abca123' str2 = 'huak3' sum_str = '' for i in str1: if i in str2: if i not in sum_str: sum_str+=i print (sum_str)