Python - Extract only characters from given string Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 out or count only the alphabetic characters. Python # Input string s = "abc123!@#XYZ" # Extract only characters result = ''.join(char for char in s if char.isalpha()) # Print the result print(result) OutputabcXYZ ExplanationThe code uses a generator expression within join() to iterate through each character in the string and checks if it is alphabetic using char.isalpha(). Only letters are included in the resulting string.The final string contains only the alphabetic characters from the input, omitting any digits or special characters.Using Regular Expressions (re.sub)Using Regular Expressions (re.sub) is an efficient way to process strings by replacing specific patterns. For extracting alphabetic characters, re.sub can remove non-letter characters, providing a streamlined approach for cleaning or filtering text. Python import re # Input string s = "abc123!@#XYZ" # Extract only characters using regex result = re.sub(r'[^a-zA-Z]', '', s) # Print the result print(result) OutputabcXYZ ExplanationThe regular expression [^a-zA-Z] matches any character that is not a letter (uppercase or lowercase). The ^ indicates negation, and the range a-zA-Z specifies alphabetic characters.The re.sub function replaces all non-alphabetic characters with an empty string (''), effectively extracting only the letters from the input string.Using List ComprehensionUsing List Comprehension involves iterating over each character in the string and conditionally including only the alphabetic characters in the result. Python # Input string s = "abc123!@#XYZ" # Extract only characters using list comprehension result = ''.join([char for char in s if char.isalpha()]) # Print the result print(result) OutputabcXYZ Explanationlist comprehension iterates over each character in the input string and checks if it is alphabetic using char.isalpha(). Only alphabetic characters are included in the resulting list.join() method concatenates the filtered characters into a single string, effectively removing non-alphabetic characters from the input string. Comment More infoAdvertise with us Next Article Python - Extract range characters from String G garg_ak0109 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads 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 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 characters except of K string Sometimes, while working with Python strings, we can have a problem in which we require to extract all the elements of string except those which present in a substring. This is quite common problem and has application in many domains including those of day-day and competitive programming. Lets discu 6 min read Get Last N characters of a string - Python We are given a string and our task is to extract the last N characters from it. For example, if we have a string s = "geeks" and n = 2, then the output will be "ks". Let's explore the most efficient methods to achieve this in Python.Using String Slicing String slicing is the fastest and most straigh 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 Python | Return lowercase characters from given string Sometimes, while working with strings, we are concerned about the case sensitivity of strings and might require getting just a specific case of character in a long string. Let's discuss certain ways in which only lowercase letters can be extracted from a string. Method #1: Using list comprehension + 5 min read Like