First, demand: to include the number or the longest letter string and length
org ='Ss121*2222&sdfs2!aaabb'result= []#Save the final string to outputResult_temp = []#saves the current longest stringMax_len = 0#Save the length of the longest string forCinchORG +' ':#Add another loop, in order to execute the else one more time, otherwise, if the end of the string satisfies the condition, it will not be saved to result ifC.isalnum ():#if C is a number or letter, add the Result_tempResult_temp.append (c)Else:#determine the length of the current result_temp until a non-digit and letter is encounteredLen_temp =Len (result_temp)ifLen_temp > Max_len:#if it is greater than the current maximum length, the result is emptied, and the string is added to the ReseultMax_len =len_temp result.clear () result.append ("'. Join (result_temp))elifLen_temp = = Max_len:#if it is equal to the current maximum length, it indicates that there are two strings of the same length, directly adding the string to resultResult.append ("'. Join (result_temp)) Result_temp= []#when non-numbers and letters are encountered, empty the result_temp and continue the next traversalifLen (Result) = =0:Print('no standard-compliant string')Else: Print('The length of the longest string that meets the requirements is:', Max_len)Print('The longest strings that meet the requirements are:', result)
Python contains the longest string and length of a number or letter