# 1. 输入一个字符串,打印所有奇数位上的字符(下标是1,3,5,7…位上的字符) # str1 = 'af3eaABo0921eodUI12o2tc02EWlcv0Nji' # for i in range(1,len(str1),2): # print(i,end='\t') # 2. 输入 'abcd1234 ' 输出'bd24' # str2 ='abcd1234 ' # new_str2 = '' # for i in range(1,len(str2),2): # new_str2 += str2[i] # print(new_str2) # 3.输入用户名,判断用户名是否合法(用户名长度6~10位) # str3 = input('输入用户名:') # if 6 <= len(str3) <=10: # print('用户名合法') # else: # print('用户名不合法') # 4.输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成) # > 例如: ** 'abc' ** — 合法 ** '123' ** — 合法 **‘abc123a’ ** — 合法 # str4 = input('输入用户名:') # for i in str4: # if '0' <= i <= '9' or 'A' <= i <= 'Z'or 'a' <= i <= 'z': # continue # pass # else: # print('用户名不合法') # break # else: # print('用户名合法') # # 5.输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母) # > 例如: ** 'abc' ** — 不合法 ** 'Mabc' ** — 不合法 ** '123' ** — 不合法 ** 'abc123' ** — 不合法 ** 'Abc123ahs' ** — 合法 # sz = False # str5 = input('输入用户名:') # for i in str5: # if 'A' >= str5[0] or str5[0] >= "Z": # print('用户名不合法') # break # if '0' <= i <= '9': # sz = True # if '0' <= i <= '9' or 'A' <= i <= 'Z'or 'a' <= i <= 'z': # continue # pass # else: # print('用户名不合法') # break # else: # if sz: # print('用户名合法') # else: # print('用户名不合法') # # 6. # 输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串 # > 例如:输入 ** 'abc1shj23kls99+2kkk' ** 输出: ** '123992' ** # str6 = 'abc1shj23kls99+2kkk' # new_str6 = '' # for i in str6: # if '0'<= i <= '9': # new_str6 += i # print(new_str6) # # 7.# 输入一个字符串,将字符串中所有的小写字母变成对应的大写字母输出(用upper方法和自己写算法两种方式实现),例如: 输入 ** 'a2h2klm12+' ** 输出 ** 'A2H2KLM12+' ** # str7 = 'a2h2klm12+' # new71_str = str7.upper() # print(new71_str) # # new72_str = '' # for i in str7: # if 'a' <= i <= 'z': # new72_str += chr(ord(i)-32) # continue # new72_str += i # # print(new72_str) # # 8. 输入一个小于1000的数字,产生对应的学号, 例如: 输入 ** '23' **,输出 ** 'py1901023' ** 输入 ** '9' **, 输出 ** 'py1901009' ** 输入 ** '123' **,输出 ** 'py1901123' ** # num = input('请输入一个小于1000的数字:') # str8 = '' # str8 = 'py1901' + '0'*(3-len(num)) + num # print(str8) # # 9.输入一个字符串,统计字符串中非数字字母的字符的个数, 例如: 输入 ** 'anc2+93-sj胡说' ** 输出: ** 4 ** 输入 '===' 输出: 3 # num = 0 # str9 = 'anc2+93-sj胡说' # for i in str9: # if '0' <= i <= '9' or 'A' <= i <= 'Z'or 'a' <= i <= 'z': # num += 1 # print(len(str9)-num) 方法二 # num = 0 # str9 = 'anc2+93-sj胡说' # for i in str9: # if not('0' <= i <= '9' or 'A' <= i <= 'Z'or 'a' <= i <= 'z'): # num += 1 # print(num) # # 10. 输入字符串,将字符串的开头和结尾变成'+',产生一个新的字符串, 例如: 输入字符串 'abc123' , 输出 ** '+bc12+' ** # str10 = 'abc123' # new_str10 = '' # for i in range(len(str10)): # if i == 0: # new_str10 += '+' # elif i == len(str10)-1: # new_str10 += '+' # else: # new_str10 += str10[i] # print(new_str10) 方法二 str10 = 'abc123' new_str10 = '+' + srt10[1:-1] + '+' print(new_str10) # # 11.输入字符串,获取字符串的中间字符, 例如: 输入 ** 'abc1234' ** 输出: ** '1' ** 输入 ** 'abc123' ** 输出 ** 'c1' ** # str11 = 'abc1234' # str11 = 'abc123' # num = len(str11) # if num % 2 != 0: # print(str11[num//2]) # else: # print(str11[num//2-1]+str11[num//2]) # # 12. # 写程序实现字符串函数find / index的功能(获取字符串1中字符串2第一次出现的位置)> 例如: 字符串1为: ** how are you? Imfine, Thankyou! **, 字符串2为: ** you **, 打印 ** 8 ** str121 = 'how are you? Imfine, Thankyou!' str122 = 'you' for i in range(len(str121)-len(str122)+1): str123 = str121[i:len(str122)+i] print(str123) if str123 == str122: print(i) break else: print('没有符合的目标')# 13. 获取两个字符串中公共的字符,> 例如: 字符串1为: ** abc123 **, 字符串2为: ** huak3 **, 打印: ** 公共字符有: a3 ** # str131 = 'abc123' # str132 = 'huak3' # str13 = '' # new_str131 = set(str131) # new_str132 = set(str132) # new_str133 = new_str131&new_str132 # for i in new_str133: # str13 += i # print(str13)
方法二
print('',join.str13(set(set(str131)&set(set(str132))
# 方法三
for i in str131:
if i in str132 and i not in str13:
str13 += i
print(str13)