-
輸入一個(gè)字符串腕侄,打印所有奇數(shù)位上的字符(下標(biāo)是1时呀,3,5晴玖,7…位上的字符)
例如: 輸入'abcd1234 ' ** 輸出'bd24'**
string = 'safhi234' for index in string[1::2]: print(str(index)
string = 'efsfwer gfdsg3432535' length = len(string) for index in range(1, length, 2): print(index, string[index])
-
輸入用戶名读存,判斷用戶名是否合法(用戶名長度6~10位)
name = input('輸入用戶名(長度在6~10位):') name_length = len(name) if 6 <= name_length <= 10: print('OK') else: print('error')
-
輸入用戶名,判斷用戶名是否合法(用戶名中只能由數(shù)字和字母組成)
例如: 'abc' — 合法 '123' — 合法 ‘a(chǎn)bc123a’ — 合法
name = input('輸入用戶名(只能由字母或者數(shù)字組成):') for item in name: if not ('a' <= item <= 'z' or 'A' <= item <= 'Z' or '0' <= item <= '9'): print('error') break else: print('OK')
-
輸入用戶名窜醉,判斷用戶名是否合法(用戶名必須包含且只能包含數(shù)字和字母宪萄,并且第一個(gè)字符必須是大寫字母)
例如: 'abc' — 不合法 '123' — 不合法 'abc123' — 不合法 'Abc123ahs' — 合法
name = input('輸入用戶名(只能由字母和數(shù)字組成,第一個(gè)數(shù)必須是字母且大寫):') if 'A' <= name[0] <= 'Z': count = 0 for item in name[1:]: if 'a' <= item <= 'z' or 'A' <= item <= 'Z' or '0' <= item <= '9': if '0' <= item <= '9': count += 1 else: print('error') break else: if count > 0: print('OK') else: print('error') else: print('error')
-
輸入一個(gè)字符串榨惰,將字符串中所有的數(shù)字字符取出來產(chǎn)生一個(gè)新的字符串
例如:輸入'abc1shj23kls99+2kkk' 輸出:'123992'
string = '`11fsf39fds8323 ew745' new_string = '' for item in string: if '0' <= item <= '9': new_string += item print(new_string)
-
輸入一個(gè)字符串拜英,將字符串中所有的小寫字母變成對應(yīng)的大寫字母輸出
例如: 輸入**'a2h2klm12+' ** 輸出 'A2H2KLM12+'
string = 'sFFs123`09fsna' strings = '' for item in string: if 'a' <= item <= 'z': item = chr(ord(item) - 32) strings += item print(strings)
-
輸入一個(gè)小于1000的數(shù)字,產(chǎn)生對應(yīng)的學(xué)號
例如: 輸入'23'琅催,輸出'py1901023' 輸入'9', 輸出'py1901009' 輸入'123'居凶,輸出'py1901123'
while True: n = input('一個(gè)數(shù)字:') fixation = 'py1903' n_fix =n.zfill(3) full_fix = fixation + n_fix print(full_fix)
-
輸入一個(gè)字符串,統(tǒng)計(jì)字符串中非數(shù)字字母的字符的個(gè)數(shù)
例如: 輸入'anc2+93-sj胡說' 輸出:4 輸入'===' 輸出:3
string = 'sasf23`+-/你好' count = 0 for item in string: if not ('a'<=item<='z' or 'A'<=item <='Z' or '0'<= item<='9'): count += 1 print(count)
-
輸入字符串藤抡,將字符串的開頭和結(jié)尾變成'+'侠碧,產(chǎn)生一個(gè)新的字符串
例如: 輸入字符串'abc123', 輸出'+bc12+'
string = 'weoirh234255'
new_str = '+' + string[1:-1] + '+'
print(new_str)
string = 'safsihq23w45'
strings = ''
string_long = len(string)
i = 0
for item in string:
i += 1
if i == 1 or i == string_long:
item = '+'
strings += item
print(strings)
- 輸入字符串,獲取字符串的中間字符
例如: 輸入'abc1234' 輸出:'1' 輸入'abc123' 輸出'c1'
string = 'abc123'
length = len(string)
if length%2 ==0:
print(string[int((length/2-1)):int((length/2+1)):1])
else:
print(string[int((length-1)/2)])
?