

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find longest consecutive letter and digit substring in Python
A given string may be a mixture of digits and letters. In this article we are required to find the biggest substring that has letters and digits together.
with re module
The regular expression module can be used to find all the continuous substring having digits or letters. Then we apply the max function to pick only those continuous substrings of letters and digits which have maximum length among all the substrings found. The findall function is also use to identify and get the required substrings.
Example
import re def longSubstring(str): letter = max(re.findall(r'\D+', str), key=len) digit = max(re.findall(r'\d+', str), key=len) return letter, digit str = 'Hello 459 Congratulations! 234' print(longSubstring(str))
Output
Running the above code gives us the following result −
(' Congratulations! ', '459')
With len() and While Loop
This is a straight but slow approach in which we design while loops to check the length of digits and letters present as substring in the given string. Then we compare their lengths and choose only the substrings with maximum length.
Example
def longSubstring(s): max_letterSeq = '' max_digitSeq = '' i = 0 while (i < len(s)): current_letterSeq = '' current_digitSeq = '' # Letters while (i < len(s) and s[i].isalpha()): current_letterSeq += s[i] i += 1 # Digits while (i < len(s) and s[i].isdigit()): current_digitSeq += s[i] i += 1 # Check if not digit or alphabet if (i < len(s) and not (s[i].isdigit()) and not (s[i].isalpha())): i += 1 if (len(current_letterSeq) > len(max_letterSeq)): max_letterSeq = current_letterSeq if (len(current_digitSeq) > len(max_digitSeq)): max_digitSeq = current_digitSeq return max_letterSeq, max_digitSeq str = 'Hello 459 Congratulations! 234' print(longSubstring(str))
Output
Running the above code gives us the following result −
('Congratulations', '459')
- Related Questions & Answers
- Program to find longest awesome substring in Python
- Longest Consecutive Sequence in Python
- Longest Palindromic Substring in Python
- Program to find longest nice substring using Python
- Finding the longest common consecutive substring between two strings in JavaScript
- Program to find length of longest consecutive sequence in Python
- Program to find length of longest palindromic substring in Python
- Longest Palindromic Substring
- Program to find length of longest consecutively increasing substring in Python
- SequenceMatcher in Python for Longest Common Substring.
- Longest Substring Without Repeating Characters in Python
- Program to find longest substring of all vowels in order in Python
- Program to find length of substring with consecutive common characters in Python
- Longest Repeating Substring in C++
- Longest Duplicate Substring in C++