You can use this pattern, too, with regex:

import re
a = '''hello? there A-Z-R_T(,**), world, welcome to python.
this **should? the next line#followed- by@ an#other %million^ %%like $this.'''

for k in a.split("\n"):
    print(re.sub(r"[^a-zA-Z0-9]+", ' ', k))
    # Or:
    # final = " ".join(re.findall(r"[a-zA-Z0-9]+", k))
    # print(final)

Output:

hello there A Z R T world welcome to python 
this should the next line followed by an other million like this 

Edit:

Otherwise, you can store the final lines into a list:

final = [re.sub(r"[^a-zA-Z0-9]+", ' ', k) for k in a.split("\n")]
print(final)

Output:

['hello there A Z R T world welcome to python ', 'this should the next line followed by an other million like this ']
Answer from Chiheb Nexus on Stack Overflow
Discussions

python - Remove all special characters, punctuation and spaces from string - Stack Overflow
I need to remove all special characters, punctuation and spaces from a string so that I only have letters and numbers. More on stackoverflow.com
🌐 stackoverflow.com
How do I remove remove stuff like (. , ' *) from a string?
You can use isalnum() to remove special characters. The string method .isalnum() returns True or False if a string is all alphanumeric characters. With this we can write a comprehension that loops over the characters in the string and only keep the alphanumeric characters. I think the regex method is more efficient, especially for really big strings, but you have to know all the characters you want to remove, with isalnum you don't. On the flipside you don't have to add another import if you use isalnum. So it depends on the situation for which method you should choose (re vs isalnum). s = "Bob 'hit' a ball, the hit BALL flew far after it was hit." # Remove all special characters unless it is a space # Replace "hit" with "sample" new_s = "".join(char for char in s if char.isalnum() or char == " ").replace("hit", "sample") print(new_s) >>>Bob sample a ball the sample BALL flew far after it was sample More on reddit.com
🌐 r/learnpython
14
1
August 6, 2021
regex - How to remove all special characters except spaces and dashes from a Python string? - Stack Overflow
I want to strip all special characters from a Python string, except dashes and spaces. Is this correct? import re my_string = "Web's GReat thing-ok" pattern = re.compile('[^A-Za-z0-9 -]') More on stackoverflow.com
🌐 stackoverflow.com
python - remove special character from string, not replace them with space - Stack Overflow
I'm trying to remove special characters from a string. All the examples available only replaces them with space. But i want to get rid of them and retain the order of the string. Below are some codes More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › @ryan_forrester_ › remove-special-characters-from-strings-in-python-complete-guide-53651c8163d9
Remove Special Characters from Strings in Python: Complete Guide | by ryan | Medium
January 7, 2025 - The `strip()` method is perfect for cleaning up the beginning and end of strings. When you need more control over character removal, regular expressions are your friend. Here’s a practical example: import re def clean_text(text): # Removes all special characters except spaces and alphanumeric characters cleaned = re.sub(r'[^a-zA-Z0-9\s]', '', text) return cleaned # Real-world example: Cleaning a product description product_desc = "Latest iPhone 13 Pro (128GB) - $999.99 *Limited Time Offer!*" clean_desc = clean_text(product_desc) print(clean_desc) # Output: "Latest iPhone 13 Pro 128GB 999.99 Limited Time Offer"
🌐
Python Guides
pythonguides.com › remove-special-characters-except-for-space-from-a-string-in-python
Remove Special Characters From a String in Python
September 5, 2025 - This method removes special characters by iterating through a list of symbols and replacing each one with an empty string using replace() in Python. Combining the join() method with a generator in Python allows us to create a new string consisting of only alphanumeric characters and spaces, effectively removing special characters.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-removing-unwanted-characters-from-string
Remove Special Characters from String in Python - GeeksforGeeks
July 11, 2025 - Explanation: re.sub() function replaces all non-alphanumeric characters matched by the pattern [^a-zA-Z0-9] with an empty string, leaving only letters and digits in the result.
🌐
CodeSpeedy
codespeedy.com › home › remove special characters from a string except space in python
Remove special characters from a string except space in Python
December 8, 2022 - Python has a built-in module known as re which is imported with a lot of functions like findall(), search(), split() etc. ... We are going to use findall() function which finds a pattern then replaces the pattern and returns the string. ... import re string = '''Hello! We% are %#$%^ trying ^&%$to remove^& special! characters%^ from*( a(^ string": except{} for_a space.''' for a in string.split("\n"): string1 = " ".join(re.findall(r"[a-zA-Z0-9]+", a)) print(string1)
Find elsewhere
🌐
Tutorial Reference
tutorialreference.com › python › examples › faq › python-how-to-remove-special-characters-from-string-except-space
How to Remove Special Characters Except Space from String in Python | Tutorial Reference
Good for the basic case of keeping ... characters while preserving spaces from Python strings is most effectively done using regular expressions with re.sub(r'[^a-zA-Z0-9\s]+', '', input_string)....
🌐
Reddit
reddit.com › r/learnpython › how do i remove remove stuff like (. , ' *) from a string?
r/learnpython on Reddit: How do I remove remove stuff like (. , ' *) from a string?
August 6, 2021 -

I have a string:

"Bob 'hit' a ball, the hit BALL flew far after it was hit."

And I want to replace the word 'hit' by 'sample'.

How do I do that when the word gets immediately followed/preceded by stuff like (. , ' *)

Edit: I know using replace() will work but I don't know what special characters there will be in the string. Some strings had characters like ( - [ / + .

🌐
datagy
datagy.io › home › python posts › python strings › python: remove special characters from a string
Python: Remove Special Characters from a String • datagy
December 17, 2022 - One of these methods is the .sub() method that allows us to substitute strings with another string. One of the perks of the re library is that we don’t need to specify exactly what character we want to replace.
🌐
TutorialsPoint
tutorialspoint.com › article › How-to-remove-all-special-characters-punctuation-and-spaces-from-a-string-in-Python
How to remove all special characters, punctuation and spaces from a string in Python?
May 2, 2025 - The re module provides powerful regular expression support in Python. The re.sub() method accepts a pattern, a replacement string, and a string as parameters, replacing pattern matches with the replacement string. ... import re text = "Welcome #@ !! to Tutorialspoint123" cleaned = re.sub('[^A-Za-z0-9]+', '', text) print(cleaned) ... The str.isalnum() method checks whether a character is alphanumeric (letter or digit). Combined with list comprehension, we can filter out unwanted characters efficiently.
🌐
Delft Stack
delftstack.com › home › howto › python › remove special characters from string python
How to Remove Special Characters From the String in Python | Delft Stack
February 2, 2024 - Next, we define a lambda function ... space, and an empty string otherwise. The lambda function performs character-wise filtering, removing all non-alphanumeric characters except spaces....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-all-characters-except-letters-and-numbers
Remove All Characters Except Letters and Numbers - Python - GeeksforGeeks
October 29, 2025 - Using a for loop, we can iterate through each character in a string and check if it is alphanumeric.
🌐
Javatpoint
javatpoint.com › how-to-remove-all-special-characters-from-a-string-in-python
How to Remove All Special Characters from a String in Python - Javatpoint
April 21, 2023 - How to Remove All Special Characters from a String in Python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.