Given a String, extract characters only which lie between given letters.
Input : test_str = ‘geekforgeeks is best’, strt, end = “g”, “s”
Output : gkorgksiss
Explanation : All characters after g and before s are retained.Input : test_str = ‘geekforgeeks is best’, strt, end = “g”, “r”
Output : gkorgki
Explanation : All characters after g and before r are retained.
Method #1 : Using list comprehension
In this, we check for character in range using comparison operation and list comprehension does task of iteration and creation of new list. Then join() can be employed to reconvert to string.
Python3
# Python3 code to demonstrate working of # Extract range characters from String # Using list comprehension # initializing string test_str = 'geekforgeeks is best' # printing original string print("The original string is : " + str(test_str)) # initializing range letters strt, end = "f", "s" # join() to get result in string res = ''.join([chr for chr in test_str if chr >= strt and chr <= end]) # printing result print("Extracted String : " + str(res)) |
The original string is : geekforgeeks is best Extracted String : gkforgksiss
Method #2 : Using filter() + lambda + join()
Another way to solve this, in this, we perform task of comparision using lambda, and filter() used lambda function to get required characters.
Python3
# Python3 code to demonstrate working of # Extract range characters from String # Using filter() + lambda + join() # initializing string test_str = 'geekforgeeks is best' # printing original string print("The original string is : " + str(test_str)) # initializing range letters strt, end = "f", "s" # join() to get result in string res = ''.join(list(filter(lambda chr : chr >= strt and chr <= end, test_str))) # printing result print("Extracted String : " + str(res)) |
The original string is : geekforgeeks is best Extracted String : gkforgksiss
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python program to extract characters in given range from a string list
- Python | Extract only characters from given string
- Python | Extract characters except of K string
- Python - Extract range of Consecutive Similar elements ranges from string list
- Python - Extract Upper Case Characters
- Python - Extract elements with Range consecutive occurrences
- Python - Extract range sized strings
- Python - Extract tuples with elements in Range
- Python program to print k characters then skip k characters in a string
- Python Regex to extract maximum numeric value from a string
- Python | Extract words from given string
- Python | Extract numbers from string
- Python | Extract digits from given string
- Python | Extract length of longest string in list
- Python | Extract Score list of String
- Python | Extract odd length words in String
- Python | Extract Numbers in Brackets in String
- Python - Extract String till Numeric
- Python - Extract words starting with K in String List
- Python - Extract Percentages from String
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.

