"""author = 喻朝東"""
1.輸入一個字符串从诲,打印所有奇數(shù)位上的字符(下標(biāo)是1妖泄,3,5混驰,7…位上的字符)
str1 = input("請輸入一個字符串:")
for index in range(len(str1)):
if index % 2 == 0:
print(str1[index],end = ' ')
print()
2.輸入用戶名锣尉,判斷用戶名是否合法(用戶名長度6~10位)
str1 = input("請輸入一個用戶名:")
if 6 <= len(str1) <= 10:
print("合法!")
else:
print("不合法刻炒!")
3.輸入用戶名,判斷用戶名是否合法(用戶名中只能由數(shù)字和字母組成) 例如: 'abc' — 合法 '123' — 合法 ‘a(chǎn)bc123a’ — 合法
str1 = input("請輸入一個用戶名:")
# str1('qwer33')
# print(len(str1))
for index in range(len(str1)):
if ('0' <= str1[index] <= '9') or ('a' <= str1[index] <= 'z') or ('A' <= str1[index] <= 'Z'):
while index == len(str1) - 1:
print("合法自沧!")
break
else:
print("不合法坟奥!")
break
4 輸入用戶名,判斷用戶名是否合法(用戶名必須包含且只能包含數(shù)字和字母拇厢,并且第一個字符必須是大寫字母)
str1 = input("請輸入用戶名:")
count = 0
count1 = 0
for index in range(len(str1)):
if (str1[0]< 'A') or (str1[0] > 'Z'):
print("不合法爱谁!")
break
else:
if '0' <= str1[index] <= '9':
count += 1
elif ('a' <=str1[index] <= 'z') or 'A'<= str1[index] <= 'Z':
count1 += 1
while index == len(str1) - 1:
if 1 <= count and count1 >= 1:
print("合法!")
break
5 輸入一個字符串孝偎,將字符串中所有的數(shù)字字符取出來產(chǎn)生一個新的字符串
例如:輸入'abc1shj23kls99+2kkk' 輸出:'123992'
str1 = input("請輸入一個字符串:")
str2 = ''
for char in str1:
if '0' <= char <= '9':
str2 += char
print(str2)
6.輸入一個字符串访敌,將字符串中所有的小寫字母變成對應(yīng)的大寫字母輸出
例如: 輸入'a2h2klm12+' 輸出 'A2H2KLM12+'
str1 = input("請輸入一個字符串:")
for char in str1:
if 'a' <= char <= 'z':
char = ord(char) - 32
print(chr(char), end='')
continue
print(char, end='')
7.輸入一個小于1000的數(shù)字,產(chǎn)生對應(yīng)的學(xué)號
例如: 輸入'23'衣盾,輸出'py1901023' 輸入'9', 輸出'py1901009' 輸入'123'寺旺,輸出'py1901123'
str1 = 'py1901'
num = input('請輸入你的學(xué)號:')
str2 = str(num).rjust(3,'0')
print('學(xué)號是:',str1 + str2)
8 輸入一個字符串爷抓,統(tǒng)計(jì)字符串中非數(shù)字字母的字符的個數(shù)
例如: 輸入'anc2+93-sjd胡說' 輸出:4 輸入'===' 輸出:3
str1 = input('請輸入一個字符串:')
count = 0
num = 0
for char in str1:
if '0' <= char <= '9'or 'a' <= char <= 'z'or 'A' <= char <= 'Z':
count += 1
num = len(str1) - count
print(num)
9 輸入字符串,將字符串的開頭和結(jié)尾變成'+'阻塑,產(chǎn)生一個新的字符串
例如: 輸入字符串'abc123', 輸出'+bc12+'
str1 = input('請輸入一個字符串:')
str2 = '+'
str3 = ''
index = 1
while 0 < index < len(str1):
str3 = str3 + str1[index]
index += 1
str3 = str2 + str3 + str2
print(str3)