Regex to the rescue!

import re

s = re.sub('[^0-9a-zA-Z]+', '*', s)

Example:

>>> re.sub('[^0-9a-zA-Z]+', '*', 'h^&ell`.,|o w]{+orld')
'h*ell*o*w*orld'
Answer from nneonneo on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @shamzaibrahim7 โ€บ three-techniques-to-remove-non-alphanumeric-characters-for-palindrome-problem-9ab7a78d8490
Three Techniques to Remove Non-Alphanumeric Characters for Palindrome Problem | by Syed Hamza | Medium
March 27, 2023 - To use regular expressions to remove non-alphanumeric characters, we can define a regular expression pattern that matches all non-alphanumeric characters, and then use the re.sub() function to replace all matches with an empty string.
Discussions

regex - Replace non alphanumeric characters except some exceptions python - Stack Overflow
0 How to remove leading and trailing non-alphanumeric characters of a certain string in python using regex? 0 Replace all Non-Alphanumeric Characters except one particular pattern using RegEx in Python More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 22, 2017
Stripping everything but alphanumeric chars from a string in Python - Stack Overflow
158 Replace all non-alphanumeric characters in a string ... 0 How to remove leading and trailing non-alphanumeric characters of a certain string in python using regex? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Replacing non-alphanumeric characters in regex match using Python - Stack Overflow
I have a text file (verilog) that contains certain string sequences (escaped identifiers) that I want to modify. In the example below, I want to find any group starting with '\' and ending with ' '... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python Regex - Replacing Non-Alphanumeric Characters AND Spaces with Dash - Stack Overflow
I am trying to replace all of the non-alphanumeric characters AND spaces in the following Python string with a dash -. I tried to use the below code, but it only replaced the non-alphanumeric char... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ remove-non-alphanumeric-characters-python
Removing Non-Alphanumeric Characters in Python - Flexiple
March 21, 2024 - Using regular expressions in Python offers a powerful and flexible approach to removing non-alphanumeric characters from strings. The re module, which stands for regular expressions, provides a method called re.sub(). This method is ideal for substituting a pattern in a string with something else โ€“ in this case, replacing non-alphanumeric characters with an empty string.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-filter-out-non-alphanumeric-characters-from-python-strings-415420
How to filter out non-alphanumeric characters from Python strings | LabEx
Let's create a new file called ... print(f"Original: {text}") print(f"Filtered: {filtered}") print("-" * 40) The regex pattern [^a-zA-Z0-9] matches any character that is NOT an uppercase letter, lowercase letter, or ...
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ python โ€บ remove non-alphanumeric characters in python
Remove Non-alphanumeric Characters in Python [3 Ways] - Java2Blog
November 10, 2023 - flag โ€“ It represents one or multiple regex flags, used to manipulate the patternโ€˜s standard behaviour. However, we used the first three parameters with the re.sub() method to replace all the occurrences of non-alphanumeric characters.
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ python โ€บ remove non-alphanumeric characters from a python string
Remove non-alphanumeric characters from a Python string | Techie Delight
3 weeks ago - This post will discuss how to remove non-alphanumeric characters from a string in Python... A simple solution is to use regular expressions for removing non-alphanumeric characters from a string.
Find elsewhere
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ remove non alphanumeric characters python
How to Remove Non-Alphanumeric Characters From Python String | Delft Stack
February 2, 2024 - After applying the regular expression pattern r"[^a-zA-Z0-9]", which matches non-alphanumeric characters, and replacing them with an empty string "", the code prints the modified string.
๐ŸŒ
Data Science Parichay
datascienceparichay.com โ€บ home โ€บ blog โ€บ python โ€“ remove non alphanumeric characters from string
Python - Remove Non Alphanumeric Characters from String - Data Science Parichay
December 16, 2021 - For example, we can write a regular expression to match with all the non-alphanumeric characters in the string and then replace them with an empty string. You can use the re library in Python to implement regular expression pattern matching.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-remove-all-characters-except-letters-and-numbers
Remove All Characters Except Letters and Numbers - Python - GeeksforGeeks
October 29, 2025 - Below are some of the most efficient methods to achieve this in Python. re.sub() function replaces all characters that match a given pattern with a replacement. Itโ€™s perfect for pattern-based filtering.
๐ŸŒ
Learning About Electronics
learningaboutelectronics.com โ€บ Articles โ€บ How-to-extract-non-alphanumeric-characters-from-a-string-in-Python-using-regular-expressions.php
How to Extract Only Non-Alphanumeric Characters from a String in Python Using Regular Expressions
This regular expression above will only have non-alphanumeric characters returned. It will not return any other type of character. To get the full picture, let's look at a complete example. This is shown in the code below. So the first thing is that in order to use regular expressions in Python, you have to import the re module.
๐ŸŒ
Alightmotionapkpro
nadeauinnovations.com โ€บ beranda
Python Tricks: Replace All Non-alphanumeric Characters in a String | Nadeau Innovations
December 31, 2022 - We have three choices: two Pythonic ways and one overkill way. Letโ€™s also assume that we want to keep whitespace characters. A non-optimized, but explicit and easy to read approach is to simply use list comprehension to strip a string of non-alphanumeric characters.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-remove-non-alphanumeric-characters-from-string
Remove non-alphanumeric characters from a Python string | bobbyhadz
April 9, 2024 - The re.sub() method will remove all non-alphanumeric characters from the string by replacing them with empty strings.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how can i remove all non alphabetic characters from my list of strings [python]
r/learnprogramming on Reddit: How can I remove all NON alphabetic characters from my list of strings [PYTHON]
March 11, 2015 -

FYI I do want to keep the commas between strings in the list. Here is my code right now. I've been working on this for hours and I'm stuck, I can't figure it out. I working on a program that will append every string of at least four characters to a new list. I'm going to use this to run through some ebooks I have in .txt

def parse(s):
    listOfWords = []
    i = 0
    final = s.lower()
    final = final.split()
    while i < len(final):
        if len(final[i]) >= 4:
                listOfWords.append(final[i])
        i = i + 1
    return listOfWords

Current Output

['lincolnโ€™s', 'silly,', 'flat', 'dishwatery', 'utterances', 'chicago',    'times,', '1863']

Desired Output

["lincoln", "silly", "flat", "dishwatery", "utterances", "chicago", "times"]
๐ŸŒ
Quora
quora.com โ€บ How-can-I-delete-non-alphabetic-characters-in-a-Python-list
How to delete non-alphabetic characters in a Python list - Quora
Answer (1 of 4): You have lots of options, one at a time, e.g.: [code]text = [c for c in text if c.isapha()] # Assuming it really is a list of chars to a list of chars gives a list text = ''.joint(text) # will give you back a string. [/code]or [code]import re text = re.sub('[^A-Za-z]', '', text...
๐ŸŒ
Replit
replit.com โ€บ discover โ€บ how-to-remove-non-alphanumeric-characters-in-python
Discover | Replit
Build and deploy software collaboratively with the power of AI without spending a second on setup.