-
Enter a string and print all odd-numbered characters (subscripts are characters in 1, 3, 5, 7...)
For example: input 'abcd1234' output 'bd24'
str1 = 'abcd1234' for i in range(len(str1)): if i % 2 != 0: print(str1[i])
- Enter the user name to determine whether the user name is legal (the length of the user name is 6~10 digits)
name = input("please enter user name") if 6 <= len(name) <= 10: print("Username is legal") else: print("Username is invalid")
-
Enter the user name to determine whether the user name is legal (the user name can only be composed of numbers and letters)
Example: 'abc' - valid '123' - valid 'abc123a' - valid
name = input("please enter user name") for i in name: if 'A' <= i <= 'z': continue elif '0' <= i <='9': continue else: print("illegal") break else: print('legitimate')
-
Input a string, extract all the numeric characters in the string to generate a new string
For example: input **'abc1shj23kls99+2kkk'** output: '123992'
str1 = 'abc1shj23kls99+2kkk' s = '' for i in str1: if '0' <= i <='9': s += i print(s)
-
Input a string, change all lowercase letters in the string into corresponding uppercase letters and output (using the upper method and writing the algorithm yourself)
Example: input **‘a2h2klm12+’ ** output ‘A2H2KLM12+’
# method one: print('a2h2klm12+'.upper()) # Method Two: str1 = 'a2h2klm12+' s = '' for i in str1: if 'a' <= i <= 'z': s += i.upper() else: s += i print(s)
6. Enter a number less than 1000 to generate the corresponding student number
For example: input **'23', output 'py1901023'** input **'9', output 'py1901009'** input **'123', output 'py1901123'**
str1 = 'py1901' s = input('Please enter a number less than 1000') if int(s) < 10: print(str1 + '00' + s) elif int(s) < 100: print(str1 + '0' + s) else: print(str1 + s)
7. Enter a string and count the number of non-alphanumeric characters in the string
For example: input **'anc2+93-sj nonsense'** output: 4 input **'==='** output: 3
str1 = 'anc2+93-sj nonsense' count = len(str1) for i in str1: if 'A' <= i <= 'z': count -= 1 elif '0' <= i <= '9': count -= 1 print(count)
8. Enter a string, change the beginning and end of the string to '+', and generate a new string
For example: input string **'abc123', output '+bc12+'**
str1 = 'abc123' s = str1.replace(str1[0], "+") s1 = s.replace(s[-1],"+") print(s1)
9. Enter a string and get the middle character of the string
For example: input **'abc1234'** output: '1' input **'abc123'** output **'c1'**
str1 = 'abc123' a = len(str1) if a % 2 == 1: print(str1[a // 2]) else: print(str1[a//2 - 1],str1[a//2])
10. Write a program to realize the function of the string function find/index (get the position where the string 2 first appears in the string 1)
For example: string 1 is: how are you? Im fine, Thank you! , string 2 is: you, print 8
str1 = 'how are you? Im fine, Thank you!' str2 = 'you' s = str1.find(str2) print(s)
11. Get the common characters in two strings
For example: string 1 is: abc123, string 2 is: huak3, print: common characters are: a3
str1 = 'abc123' str2 = 'huak3' for i in str1: for j in str2: if i == j: print(i,end='')
12. Enter the user name to determine whether the user name is legal (the user name must contain and can only contain numbers and letters, and the first character must be an uppercase letter)
Example: 'abc' - illegal 'Mabc' - illegal '123' - illegal 'abc123' - illegal 'Abc123ahs' - legal
user_name = input('Enter your user name:') count1 = 0 count2 = 0 count3 = 0 if user_name[0].isupper(): for i in user_name[1:]: if '0' <= i <= '9': count1 += 1 elif 'a' <= i <= 'z' or 'A' <= i <= 'Z': count2 += 1 else: count3 += 1 break if count1 != 0 and count2 != 0 and count3 == 0: print('legitimate') else: print('illegal')