-
輸入一個(gè)字符串驻啤,打印所有奇數(shù)位上的字符(下標(biāo)是1框产,3贰逾,5悬荣,7…位上的字符)
例如: 輸入'abcd1234 ' ** 輸出'bd24'**
str0 = input('Please input a string: ')
for chr in range(1, len(str0), 2)
print(chr, end='')
- 輸入用戶名,判斷用戶名是否合法(用戶名長度6~10位)
str0 = input('Please input a string: ')
if not (6 <= len(str0) <= 10):
print('NameError!')
-
輸入用戶名疙剑,判斷用戶名是否合法(用戶名中只能由數(shù)字和字母組成)
例如: 'abc' — 合法 '123' — 合法 ‘a(chǎn)bc123a’ — 合法
str0 = input('Please input a string: ')
for char in str0:
if not ('0' <= char <= '9' or 'A' <= char <= 'Z' or 'a' <= char <= 'z'):
print('NameError!')
break
else:
print('CorrectName')
-
輸入用戶名氯迂,判斷用戶名是否合法(用戶名必須包含且只能包含數(shù)字和字母,并且第一個(gè)字符必須是大寫字母)
例如: 'abc' — 不合法 '123' — 不合法 'abc123' — 不合法 'Abc123ahs' — 合法
str0 = input('Please input a string: ')
for i in range(len(str0)):
if not 'A' <= str0[0] <= 'Z':
print('NameError!')
break
elif not ('0' <= str0[i] <= '9' or 'A' <= str0[i] <= 'Z' or 'a' <= str0[i] <= 'z'):
print('NameError!')
break
if i == len(str0)-1:
print('CorrectName')
-
輸入一個(gè)字符串言缤,將字符串中所有的數(shù)字字符取出來產(chǎn)生一個(gè)新的字符串
例如:輸入'abc1shj23kls99+2kkk' 輸出:'123992'
str0 = input('Please input a string: ')
str1 = ''
for char in str0:
if '0' <= char <= '9':
str1 += char
-
輸入一個(gè)字符串嚼蚀,將字符串中所有的小寫字母變成對應(yīng)的大寫字母輸出 (用upper方法和自己寫算法兩種方式實(shí)現(xiàn))
例如: 輸入**'a2h2klm12+' ** 輸出 'A2H2KLM12+'
#upper
str0 = input('Please input a string: ')
str1 = str0.upper()
#私の方法
str0 = input('Please input a string: ')
str1 = ''
for char in str0:
if 'a' <= char <= 'z':
str1 += chr(ord(char)-32)
elif 'A' <= char <= 'Z':
strl += chr(ord(char)+32)
else:
strl += char
-
輸入一個(gè)小于1000的數(shù)字,產(chǎn)生對應(yīng)的學(xué)號(hào)
例如: 輸入'23'管挟,輸出'py1901023' 輸入'9', 輸出'py1901009' 輸入'123'轿曙,輸出'py1901123'
num = input('Please input your number: ')
id_ = 'py1904'+num.zfill(3)
-
輸入一個(gè)字符串,統(tǒng)計(jì)字符串中非數(shù)字字母的字符的個(gè)數(shù)
例如: 輸入'anc2+93-sj胡說' 輸出:4 輸入'===' 輸出:3
count = 0
str0 = input('Please input a string: ')
for char in str0:
if not ('0' <= char <= '9' or 'A' <= char <= 'Z' or 'a' <= char <= 'z'):
count += 1
-
輸入字符串哮独,將字符串的開頭和結(jié)尾變成'+'拳芙,產(chǎn)生一個(gè)新的字符串
例如: 輸入字符串'abc123', 輸出'+bc12+'
str0 = input('Please input a string: ')
strl = '+' + str0[1:len(str0)-1] + '+'
- 輸入字符串,獲取字符串的中間字符
例如: 輸入'abc1234' 輸出:'1' 輸入'abc123' 輸出'c1'
str0 = input('Please input a string: ')
le = len(str0)
if le & 1:
strl = str0[le//2:(le+1)//2]
else:
str1 = str0[le//2-1:le//2+1]
- 寫程序?qū)崿F(xiàn)字符串函數(shù)find/index的功能(獲取字符串1中字符串2第一次出現(xiàn)的位置)
例如: 字符串1為:how are you? Im fine, Thank you! , 字符串2為:you, 打印8
str0 = 'chicken is so beautiful!'
str1 = "so"
le0 = len(str0)
le1 = len(str1)
for i in range(le0):
if str0[i:i+le1] == str1:
print(i)
break
if i+le1 == le0:
print('Not Fond!')
- 獲取兩個(gè)字符串中公共的字符
例如: 字符串1為:abc123, 字符串2為: huak3 , 打印:公共字符有:a3
str0 = 'qwer123[],>'
str1 = 'efs3fs<,.'
str2 = 'The common chars are:' + str(set(str0) & set(str1))