Python – Consecutive Alphabetic Occurrence

Sometimes, while working with Strings, we can have a problem in which we need to check whether we can find occurrence of characters consecutive and according to English alphabets. This kind of problem can occur in school programming and day-day programming. Lets discuss certain ways in which this task can be performed.

Method #1 : Using loop + ascii_letters + zip()
The combination of above methods can be used to perform this task. In this, we extract the English alphabets using ascii_letters and check for consecution using zip().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Consecutive Alphabetic Occurrence
# Using loop + ascii_letters + zip()
from string import ascii_letters
  
# initializing string
test_str = 'geeksforgeeks is best fgr geeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Consecutive Alphabetic Occurrence
# Using loop + ascii_letters + zip()
res = []
for i, j in zip(ascii_letters, ascii_letters[1:]) :
    if i + j in test_str:
        res.append((i, j))
  
# printing result 
print("The Consecutive matching letter pairs : " + str(res)) 

chevron_right


Output :

The original string is : geeksforgeeks is best fgr geeks
The Consecutive matching letter pairs : [(‘f’, ‘g’), (‘s’, ‘t’)]

 

Method #2 : Using list comprehension + ascii_letters + zip()
The combination of above methods can be used to perform this task. In this, we perform similar way as above just in one-liner shortened way using list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Consecutive Alphabetic Occurrence
# Using list comprehension + ascii_letters + zip()
from string import ascii_letters
  
# initializing string
test_str = 'geeksforgeeks is best fgr geeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Consecutive Alphabetic Occurrence
# Using list comprehension + ascii_letters + zip()
res = [(i, j) for i, j in zip(ascii_letters, 
      ascii_letters[1:]) if i + j in test_str]
  
# printing result 
print("The Consecutive matching letter pairs : " + str(res)) 

chevron_right


Output :

The original string is : geeksforgeeks is best fgr geeks
The Consecutive matching letter pairs : [('f', 'g'), ('s', 't')]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.