-
Enter a string and print all characters in odd bits (subscripts are characters in bits 1, 3, 5, 7...)
For example: input * *'abcd1234 '* * output * *'bd24'**
# str1 = input('Please enter a string: ') str1 = 'abcd1234' for index in range(1 ,len(str1)+1,2): print(str1[index],end='') print()
-
Enter the user name and judge whether the user name is legal (the length of the user name is 6 ~ 10 characters)
# user_name = input("please enter user name (6 ~ 10 digits):") user_name = '12345' if not 6 <= len(user_name) <= 10: print('Illegal user name!') else: print('get into!')
-
Enter the user name to judge whether the user name is legal (the user name can only be composed of numbers and letters)
For example, 'ABC' - Legal '123' - Legal 'abc123a' - Legal
# user_name= input("please enter user name (6 ~ 10 digits):") user_name = '12345 king' for string in user_name: if not ('0' <= string <= '9' or 'A' <= string <= 'Z' or 'a' <= string <= 'z'): print('wrongful!') break else: print('legitimate!')
-
Enter the user name and judge whether the user name is legal (the user name must contain and can only contain numbers and letters, and the first character must be capital letters)
For example, 'ABC' - illegal '123' - illegal 'abc123' - illegal 'Abc123ahs' - Legal
user_name = 'A12345' if not 'A' <= user_name[0] <= 'Z': print('wrongful!') else: for string in user_name[1:]: if not ('0' <= string <= '9' or 'A' <= string <= 'Z' or 'a' <= string <= 'z'): print('wrongful!') break else: print('legitimate!')
-
Enter a string and take out all the numeric characters in the string to produce a new string
For example: input * * 'abc1shj23kls99+2kkk' * * output: '123992'
str1 = 'abc1shj23kls99+2kkk' new_str = '' for string in str1: if '0'<= string<='9': new_str += string print(new_str) # 123992
-
Input a string and change all lowercase letters in the string into corresponding uppercase letters for output (implemented by upper method and self writing algorithm)
For example: input * * 'A2H2KLM12 +' * * output 'A2H2KLM12 +'
# Method 1: str1 = 'a2h2klm12+' new_str = '' for string in str1: if 'a' <= string <= 'z': new_str += chr(ord(string) - (ord('a')-ord('A'))) else: new_str += string print(new_str) # Method 2: str1 = 'a2h2klm12+' new_str = '' for string in str1: if 'a' <= string <= 'z': new_str += string.upper() else: new_str += string print(new_str)
-
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 '**
# Num = input ('Please enter a number less than 1000: ') nums = '12' str0 = 'py1901' if len(nums) == 1: print(str0 + '00' + nums) elif len(nums) == 2: print(str0 + '0' + nums) elif len(nums) == 3: print(str0 + nums) else: print('Input error!')
-
Enter a string to count the number of non numeric and alphabetic characters in the string
For example: input * * 'anc2+93-sj nonsense' * * output: 4 input * * '= =' * * output: 3
# str1 = input('Please enter a string: ') str1 = 'anc2+93-sj nonsense' count = 0 for string in str1: if not ('0'<=string<='9' or 'A' <= string <= 'Z' or 'a' <= string <= 'z'): count += 1 print(count)
-
Enter a string, change the beginning and end of the string to '+' to produce a new string
For example: input string * * 'abc123', output '+ bc12 +'**
# str1 = input('Please enter a string: ') str1 = 'abc123' length = len(str1) new_str = '+'+str1[1:length-1]+'+' print(new_str)
-
Enter a string to get the middle character of the string
For example: input * * 'abc1234' * * output: '1' input * * 'abc123' * * output * * 'c1'**
# str1 = input('Please enter a string: ') str1 = 'abc1234' length = len(str1) if length % 2: # Odd number string_mid = str1[length // 2] else: string_mid = str1[length // 2 - 1:length // 2 + 1] print(string_mid)
- The writer implements the function of the string function find/index (get the position of the first occurrence of string 2 in 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' len2 = len(str2) for index in range(len(str1)): if str1[index:index+len2] == str2: print(index) break else: print('String 2 does not exist in string 1')
- Gets 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' # print(''.join([x for x in str1 if x in [y for y in str2]])) str11 = set(str1) str21 = set(str2) str3 = '' for string in (str11 & str21): str3 += string print(str3)