- 輸入一個字符串算墨,打印所有奇數(shù)位上的字符(下標是1兔仰,3幢尚,5破停,7…位上的字符)
str_1 = input('輸入字符串:')
print(str_1[1::2])
- 輸入用戶名翅楼,判斷用戶名是否合法(用戶名長度6~10位)
- 輸入用戶名尉剩,判斷用戶名是否合法(用戶名中只能由數(shù)字和字母組成)
- 輸入用戶名,判斷用戶名是否合法(用戶名必須包含且只能包含數(shù)字和字母毅臊,并且第一個字符必須是大寫字母)
str_234 = input('輸入用戶名:')
count = 0
if 6 <= len(str_234) <= 10:
if 'A' <= str_234[0] <= 'Z':
for i in str_234:
if 0 <= ord(i) <= 9:
count += 1
elif 'A' <= i <= 'Z' or 'a' <= 'z':
if count != 0:
print(True)
else:
print('用戶名必須包含數(shù)字')
break
else:
print(False, '用戶名只能包含數(shù)字和字母')
else:
print(False, '第一個字符為大寫字母')
else:
print(False, '用戶名過長')
- 輸入一個字符串理茎,將字符串中所有的數(shù)字字符取出來產(chǎn)生一個新的字符串
str_5 = input()
new_str_5 = ''
for i in str_5:
if '0' <= i <= '9':
new_str_5 += i
- 輸入一個字符串,將字符串中所有的小寫字母變成對應的大寫字母輸出
str_6 = input()
new_str_6 = str_6.upper()
- 輸入一個小于1000的數(shù)字管嬉,產(chǎn)生對應的學號
str_7 = input('輸入一個小于1000的數(shù):')
study_ID = 'python1903' + str_7
- 輸入一個字符串皂林,統(tǒng)計字符串中非數(shù)字字母的字符的個數(shù)
str_8 = input()
count = 0
for i in str_8:
if 9 < ord(i) < ord('A') or 'Z' < i < 'a' or i > 'z':
count += 1
print(count)
- 輸入字符串,將字符串的開頭和結(jié)尾變成'+'蚯撩,產(chǎn)生一個新的字符串
str_9 = input()
new_str_9 = '+' + str_9[1:-1] + '+'
print(new_str_9)
- 輸入字符串础倍,獲取字符串的中間字符
str_10 = input()
l = len(str_10) // 2
if len(str_10) % 2 == 0:
print(str_10[(l-1): (l+1)])
else:
print(str_10[l])
最后編輯于 :
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者