
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- Program to find longest awesome substring in Python
- Longest Palindromic Substring in Python
- Longest Consecutive Sequence 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 palindromic substring in Python
- Program to find length of longest consecutive sequence in Python
- Longest Substring Without Repeating Characters in Python
- SequenceMatcher in Python for Longest Common Substring.
- Program to find length of longest consecutively increasing substring 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
- Program to find length of longest repeating substring in a string in Python
- Longest Palindromic Substring
- Program to find length of longest palindromic substring after single rotation in Python
