1. enter a string of characters printed on all odd bit (subscripted characters on bits 1,3,5,7 ...)
For example: Enter 'abcd1234' output 'bd24'
str1 = input('请输入字符串:')
for index1 in range(len(str1)):
if index1 % 2 == 1:
print(str1[index1])
2. Enter the user name, the user name is valid is determined (user name length of 6 to 10)
name1 = input('请输入用户名:')
while True:
if 6 <= len(name1) <= 10:
print('用户名合法,欢迎!')
break
else:
print('用户名不合法')
name1 = input('请输入用户名:')
3. Enter the user name, user name to determine the legality of (the user name only by numbers and letters)
For example: 'abc' - legal '123' - legal 'abc123a' - legitimate
'''方法①'''
name2 = input('请输入用户名:')
for name_2 in name2:
if not ('0' <= name_2 <= '9' or 'A' <= name_2 <= 'Z' or 'a' <= name_2 <= 'z'):
print('不合法')
break
else:
print('合法')
'''方法②'''
name2 = input('请输入用户名:')
for name_2 in name2:
if name_2.isalnum() == False:
print('不合法')
break
else:
print('合法')
4. Enter your user name, user name to determine the legality (username and must contain only contain numbers and letters, and the first character must be a capital letter)
For example: 'abc' - not legal '123' - not legal 'abc123' - not legal 'Abc123ahs' - legitimate
name3 = input('请输入用户名:')
for name_3 in name3:
if not 'A' <= name3[0] <= 'Z':
print('不合法')
break
elif not ('0' <= name_3 <= '9' or 'A' <= name_3 <= 'Z' or 'a' <= name_3 <= 'z'):
print('不合法')
break
elif name3.isalpha() == True or name3.isdigit() == True:
print('不合法')
break
else:
print('合法')
# 5. Enter a string, numeric character string taken out all produce a new string
For example: Enter 'abc1shj23kls99 + 2kkk' Output: '123992'
str2 = input('请输入:')
list1 = []
for value1 in str2:
if '0' <= value1 <= '9':
list1.append(value1)
print(list1)
print(''.join(list1))
6. Enter a string, the string in all lowercase letters capitalized corresponding to the output (by the method and upper two ways to write algorithms)
For example: Enter 'a2h2klm12 +' output 'A2H2KLM12 +'
str3 = input('请输入:')
print(str3.upper())
list2 = []
for value2 in str3:
if 'a' <= value2 <= 'z':
list2.append(chr(ord(value2) - 32))
continue
list2.append(value2)
print(''.join(list2))
7. Enter a number less than 1000, to produce the corresponding number of school
For example: Enter '23', the output 'py1901023' enter '9', outputs 'py1901009' input '123', outputs 'py1901123'
num1 = input('请输入数字:')
print('py1906', num1.rjust(3, '0'), sep='')
print('py1906', num1.zfill(3), sep='')
8. Enter a string, counting the number of strings of alphanumeric characters Africa
For example: Enter 'anc2 + 93-sj nonsense' output: Input 4 '===' Output: 3
str3 = input('请输入:')
count1 = 0
for oth in str3:
if not ('0' <= oth <='9' or 'A' <= oth <= 'Z' or 'a' <= oth <= 'z'):
count1 +=1
print(count1)
9. The input string, the beginning and end of the string into the '+', generating a new string
For example: input string 'abc123', output '+ bc12 +'
str4 = input('请输入:')
str5 = str4.replace(str4[0], '+')
print(str5.replace(str5[-1], '+'))
10. The input string, access the string of characters intermediate
For example: Enter 'ABC1234' Output: '1' input 'abc123' output 'C1'
str6 = input('请输入:')
if int(len(str6)) & 1 == 1:
print(str6[int(len(str6)//2)])
elif int(len(str6)) & 1 == 0:
print(str6[int(len(str6) // 2 - 1)], str6[int(len(str6) // 2)])
11. Write program implements string functions find / index features (to get the string 2 position of the first occurrence of a string)
For example: string 1 is: how are you Im fine, Thank you !, string 2:? You, print 8
str7 = 'how are you? Im fine, Thank you!'
str8 = 'you'
for index7 in range(len(str7)):
n = 0
for index8 in range(len(str8)):
if str7[index7] == str8[0]:
if str7[index7 + 1] == str8[1]:
print(index7)
n += 1
break
if n == 1:
break
12. Obtain two character strings in common
For example: string 1 is: abc123, string 2: huak3, printing: common character has: A3
str9 = 'abc123'
str10 = 'huak3'
set1 = set()
for value9 in str9:
for value10 in str10:
if value9 == value10:
set1.add(value9)
print('公共字符有:',''.join(set1))