輸入一個字符串旬昭,打印所有奇數(shù)位上的字符(下標是1眯漩,3旧噪,5吨娜,7…位上的字符)
例如: 輸入'abcd1234 ' 輸出'bd24'
string_1 = input('請輸入一個字符串')
for index in range(len(string_1)):
if index % 2 != 0:#奇數(shù)位
print(string_1[index])
輸入用戶名,判斷用戶名是否合法(用戶名長度6~10位)
string_2 = input('請輸入你的用戶名')
lenth_1 = len(string_2)
if lenth_1 >= 6 and lenth_1 <= 10:判斷長度是否為6到7
print('合法')
else:
print('不合法')
輸入用戶名淘钟,判斷用戶名是否合法(用戶名中只能由數(shù)字和字母組成)
例如: 'abc' — 合法 '123' — 合法 ‘a(chǎn)bc123a’ — 合法
string_3 = input('請輸入一個字符串')
count = 0
for index in string_3:
if 'a' <= index <= 'z' or 'A' <= index <= 'Z' or '0' <= index <= '9':
count = 1
else:#出現(xiàn)不是字母或者數(shù)字的字符宦赠,打印不合法
print('不合法')
count = 0
break#程序結(jié)束
if count == 1:
print('合法')
輸入用戶名,判斷用戶名是否合法(用戶名必須包含且只能包含數(shù)字和字母米母,并且第一個字符必須是大寫字母)
例如: 'abc' — 不合法 ** '123'** — 不合法 'abc123' — 不合法 'Abc123ahs' — 合法
string_4 = input('請輸入一個字符串')
num4 = ''
count = 0
if 'A' <= string_4[0] <= 'Z':
count = 1
else:
count = 0
if string_4.isalpha() != 0:#判斷字符是否全是字母
count = 0
for index in string_4:#判斷字符串是否包含除數(shù)字字母外的其他字符
if count == 0:
break
if 'a' <= index <= 'z':
count = 1
elif 'A' <= index <= 'Z':
count = 1
elif '0' <= index <= '9':
count = 1
else:
count = 0
break
if count == 1:
print('合法')
else:
print('不合法')
輸入一個字符串勾扭,將字符串中所有的數(shù)字字符取出來產(chǎn)生一個新的字符串
例如:輸入'abc1shj23kls99+2kkk' 輸出:'123992'
string_5 = input('請輸入字符串')
new_string = str()
for index in string_5:
if '0' <= index <= '9':
new_string += index
print(new_string)
輸入一個字符串,將字符串中所有的小寫字母變成對應的大寫字母輸出
例如: 輸入'a2h2klm12+' 輸出 'A2H2KLM12+'
string_6 = input('請輸入一個字符串')
new_string = ''
for index in string_6:
if 'a' <= index <= 'z':
new_string += index.upper()
else:
new_string += index
print(new_string)
輸入一個小于1000的數(shù)字铁瞒,產(chǎn)生對應的學號
例如: 輸入'23'妙色,輸出'py1901023' 輸入'9', 輸出'py1901009' 輸入'123',輸出'py1901123'
number_1 = input('請輸入一個一千以內(nèi)的數(shù)')
string_7 = 'py19010'
new_string = string_7 + number_1
print(new_string)
輸入一個字符串慧耍,統(tǒng)計字符串中非數(shù)字字母的字符的個數(shù)
例如: 輸入'anc2+93-sjd胡說' 輸出:4 輸入'===' 輸出:3
string_8 = input('請輸入一個字符串')
count = 0
for x in string_8:
if 'a' <= x <= 'z' or 'A' <= x <= 'Z' or '0' <= x <= '9':
continue
else:
count += 1
print(count)
輸入字符串身辨,將字符串的開頭和結(jié)尾變成'+',產(chǎn)生一個新的字符串
例如: 輸入字符串'abc123', 輸出'+bc12+'
string_9 = input('請輸入一個字符串')
new_string = ''
for x in range(len(string_9)):
if x == 0:
new_string += '+'
elif x == len(string_9) - 1:
new_string += '+'
else:
new_string += string_9[x]
print(new_string)
輸入字符串芍碧,獲取字符串的中間字符
例如: 輸入'abc1234' 輸出:'1' 輸入'abc123' 輸出'c1'
string_10 = input('請輸入一個字符串')
new_string = ''
for x in range(len(string_10)):
if len(string_10) % 2 == 1:
x = (len(string_10)-1)//2
print(string_10[x])
break
else:
x = len(string_10)//2
new_string = string_10[x-1] + string_10[x]
print(new_string)
break
最后編輯于 :2019.03.12 11:19:45
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者