Python contains the longest string and length of a number or letter

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.