Python | Extract Strings with only Alphabets Last Updated : 13 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In Python, extracting strings that contain only alphabetic characters involves filtering out any strings that include numbers or special characters. The most efficient way to achieve this is by using the isalpha() method, which checks if all characters in a string are alphabetic. Python li= ['gfg', 'is23', 'best', 'for2', 'geeks'] res = [sub for sub in li if sub.isalpha()] print(str(res)) Output['gfg', 'best', 'geeks'] Explanation:sub.isalpha() method is used to check if a string consists only of alphabetic characters. It returns True if the string contains only letters no numbers or special characters , otherwise it returns False.List comprehension loops through each element of the list li and checks if it passes the isalpha() test.Table of ContentUsing filter()Using regular expressionsUsing all()Using filter()filter() function uses str.isalpha() to check if each string contains only alphabetic characters, filtering the list accordingly. The result is an iterator, which can be converted to a list. It's efficient but less intuitive than list comprehension. Python li= ['gfg', 'is23', 'best', 'for2', 'geeks'] res = list(filter(str.isalpha, li)) print(res) Output['gfg', 'best', 'geeks'] Explanation:str.isalpha checks if each string in the list contains only alphabetic characters .filter() goes through each item in the list li and applies the str.isalpha() condition:list(filter(str.isalpha, li)): Since filter() returns an iterator, we need to convert it to a list using list() to get the filtered results.Using regular expressionsThis method uses regular expressions to match strings that contain only alphabetic characters. While regular expressions are versatile and powerful, they might be slightly slower than isalpha() for this simple task due to the overhead of regex processing. Python import re li = ['gfg', 'is23', 'best', 'for2', 'geeks'] res = [sub for sub in li if re.match('^[a-zA-Z]+$', sub)] print(res) Output['gfg', 'best', 'geeks'] Explanation:re.match('^[a-zA-Z]+$', sub): This is where a regular expression is used to check if a string sub contains only alphabetic characters.List comprehension loops through each string in the list li and applies the re.match() function.Using all()all() function can be used in list comprehension to check if every character in a string is alphabetic. It iterates over each character and applies isalpha() to ensure all characters are letters. Python li = ['gfg', 'is23', 'best', 'for2', 'geeks'] res = [sub for sub in li if all(c.isalpha() for c in sub)] print(res) Output['gfg', 'best', 'geeks'] Explanation:(all(c.isalpha() for c in sub)):This checks if every character in sub is alphabetic using c.isalpha() all(): This ensures all characters in sub are alphabetic.If all characters are alphabetic it returns True, otherwise False. Comment More infoAdvertise with us Next Article Python | Extract Strings with only Alphabets manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python string-programs Practice Tags : python Similar Reads Python - Strings with all given List characters GIven Strings List and character list, extract all strings, having all characters from character list. Input : test_list = ["Geeks", "Gfg", "Geeksforgeeks", "free"], chr_list = [ 'f', 'r', 'e'] Output : ['free', "Geeksforgeeks"] Explanation : Only "free" and "Geeksforgeeks" contains all 'f', 'r' and 4 min read Python - Extract only characters from given string To extract only characters (letters) from a given string we can use various easy and efficient methods in Python. Using str.isalpha() in a Loop str.isalpha() method checks if a character in a string is an alphabetic letter. Using a loop, we can iterate through each character in a string to filter ou 2 min read Python - Filter rows with only Alphabets from List of Lists Given Matrix, write a Python program to extract rows which only contains alphabets in its strings. Examples: Input : test_list = [["gfg", "is", "best"], ["Geeks4Geeks", "good"], ["Gfg is good"], ["love", "gfg"]] Output : [['gfg', 'is', 'best'], ['love', 'gfg']] Explanation : All strings with just al 5 min read Python Program to test if the String only Numbers and Alphabets Given a String, our task is to write a Python program to check if string contains both numbers and alphabets, not either nor punctuations. Examples: Input : test_str = 'Geeks4Geeks' Output : True Explanation : Contains both number and alphabets. Input : test_str = 'GeeksforGeeks' Output : False Expl 4 min read Python - Extract digits from given string We need to extract the digit from the given string. For example we are given a string s=""abc123def456gh789" we need to extract all the numbers from the string so the output for the given string will become "123456789" In this article we will show different ways to extract digits from a string in Py 2 min read Python - Extract String till Numeric Given a string, extract all its content till first appearance of numeric character. Input : test_str = "geeksforgeeks7 is best" Output : geeksforgeeks Explanation : All characters before 7 are extracted. Input : test_str = "2geeksforgeeks7 is best" Output : "" Explanation : No character extracted as 5 min read Python - Extract range characters from String 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 : gkorgk 4 min read Test if String Contains Alphabets and Spaces - Python We are given a string and the task is to determine whether it contains only alphabets and spaces, this is often required when working with datasets where the string must not include any digits, punctuation or special characters.Using all() isspace() and isalpha()This method iterates through each cha 3 min read Extract words starting with K in String List - Python In this article, we will explore various methods to extract words starting with K in String List. The simplest way to do is by using a loop.Using a LoopWe use a loop (for loop) to iterate through each word in the list and check if it starts with the exact character (case-sensitive) provided in the v 2 min read Python | Extract words from given string In Python, we sometimes come through situations where we require to get all the words present in the string, this can be a tedious task done using the native method. Hence having shorthand to perform this task is always useful. Additionally, this article also includes the cases in which punctuation 4 min read Like