Remove All Characters Except Letters and Numbers - Python Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we’ll learn how to clean a string by removing everything except letters (A–Z, a–z) and numbers (0–9). This means getting rid of:Special characters (@, #, !, etc.)Punctuation marksWhitespaceFor example, consider a string s = "[email protected]", after removing everything except the numbers and alphabets the string will become "Geeksno1".Let’s explore different ways to achieve this in Python:Using Regular Expressions (re.sub())re.sub() function replaces all characters that match a given pattern with a replacement. It’s perfect for pattern-based filtering. Python import re s1 = "Hello, World! 123 @Python$" s2 = re.sub(r'[^a-zA-Z0-9]', '', s1) print(s2) OutputHelloWorld123Python Explanation:[^a-zA-Z0-9]: Matches any character that is not a letter or number.re.sub() replaces these characters with an empty string.The result is a clean string with only alphanumeric characters..Using List Comprehension with str.isalnum()List comprehension lets you filter characters efficiently in a single line. Python s1 = "Hello, World! 123 @Python$" s2 = ''.join([char for char in s1 if char.isalnum()]) print(s2) OutputHelloWorld123Python Explanation:char.isalnum() returns True for letters and numbers.Only those characters are included and joined into a new string.Using filter() with str.isalnum()The filter() function applies a condition (in this case, isalnum) to each character and filters out the rest. Python s1 = "Hello, World! 123 @Python$" s2 = ''.join(filter(str.isalnum, s1)) print(s2) OutputHelloWorld123Python Explanation:filter(str.isalnum, s1) keeps only characters where isalnum() is True.''.join() combines the valid characters into a new string.Using a for LoopUsing a for loop, we can iterate through each character in a string and check if it is alphanumeric. If it is, we add it to a new string to create a cleaned version of the original. Python s1 = "Hello, World! 123 @Python$" s2 = '' for char in s1: if char.isalnum(): s2 += char print(s2) OutputHelloWorld123Python Explanation:The loop checks each character.If it's alphanumeric, it's added to the result string s2.Also read: Strings, List comprehension, re.sub(), regular expressions, filter, str.isalnum(), ''.join() method. Comment More infoAdvertise with us Next Article Python - Replace multiple characters at once G garg_ak0109 Follow Improve Article Tags : Python Python Programs python-regex Python string-programs Python Regex-programs +1 More Practice Tags : python Similar Reads Python Program To Remove all control characters In the telecommunication and computer domain, control characters are non-printable characters which are a part of the character set. These do not represent any written symbol. They are used in signaling to cause certain effects other than adding symbols to text. Removing these control characters is 3 min read Python - Remove N characters after K Given a String, remove N characters after K character. Input : test_str = 'ge@987eksfor@123geeks is best@212 for cs', N = 3, K = '@' Output : 'geeksforgeeks is best for cs' Explanation : All 3 required occurrences removed. Input : test_str = 'geeksfor@123geeks is best for cs', N = 3, K = '@' Output 2 min read Python - Remove all digits before given Number Given a String, remove all numeric digits before K number. Method #1 : Using split() + enumerate() + index() + list comprehension This is one of the ways in which this task can be performed. In this, we perform task of split() to get all words, getting index of K number using index() and list compre 6 min read Python - Replace multiple characters at once Replacing multiple characters in a string is a common task in Python Below, we explore methods to replace multiple characters at once, ranked from the most efficient to the least.Using translate() with maketrans() translate() method combined with maketrans() is the most efficient way to replace mult 2 min read Python - Replace multiple characters at once Replacing multiple characters in a string is a common task in Python Below, we explore methods to replace multiple characters at once, ranked from the most efficient to the least.Using translate() with maketrans() translate() method combined with maketrans() is the most efficient way to replace mult 2 min read Python - Replace multiple characters at once Replacing multiple characters in a string is a common task in Python Below, we explore methods to replace multiple characters at once, ranked from the most efficient to the least.Using translate() with maketrans() translate() method combined with maketrans() is the most efficient way to replace mult 2 min read Like