import re

new_s1 = re.sub(r'[\W_]+','_',s1)
new_s2 = re.sub(r'[\W_]+','_',s2)

Input:

s1 = 'Hi th@re gu^ys!@#()tes   t;"[]ing'
s2 = 'Hi th@re gu^ys!@#()tes   t___;"[]ing'

Output:

>>> print(new_s1)
>>> Hi_th_re_gu_ys_tes_t_ing
>>> print(new_s2)
>>> Hi_th_re_gu_ys_tes_t_ing
Answer from user19168512 on Stack Overflow
Discussions

python - How to replace special characters in file name with Underscore symbol using Unix? - Stack Overflow
I am working upon the task, where I need to replace the Special characters in the file name with _, in all the recursive folders in unix/Python. PFB the Unix code. #!/bin/sh # Shell script to fi... More on stackoverflow.com
🌐 stackoverflow.com
python - Regex, replace all underscore and special char with spaces? - Stack Overflow
so im trying to replace all special chars into spaces, using regex: my code works, but it wont replace underscore, what should i do? ... Underscore is considered a "word character" in PCRE regular expressions. If what you want to match is "anything that is not a word character or an underscore", try this: ... Sign up to request clarification or add additional context in comments. ... The underscore is part of the \w character group. Use this instead: ... @qwrrty: In Python ... More on stackoverflow.com
🌐 stackoverflow.com
How to replace underscores with letters?
I am coding a hangman game and I have it to where it shows you how many characters are in the "secret" with this command. for letter in secret: if… More on reddit.com
🌐 r/learnpython
10
1
July 1, 2020
Replace special chars with underscores for environment variables with credentials
I am on the latest stable Poetry version, installed using a recommended method. I have searched the issues of this repo and believe that this is not a duplicate. I have searched the FAQ and general documentation and believe that my quest... More on github.com
🌐 github.com
6
November 30, 2022
🌐
Stack Overflow
stackoverflow.com › questions › 60675659 › how-to-replace-special-characters-in-file-name-with-underscore-symbol-using-unix › 60679626
python - How to replace special characters in file name with Underscore symbol using Unix? - Stack Overflow
I am working upon the task, where I need to replace the Special characters in the file name with _, in all the recursive folders in unix/Python. PFB the Unix code. #!/bin/sh # Shell script to fi...
🌐
Bobby Hadz
bobbyhadz.com › blog › python-replace-spaces-with-underscores
How to replace Spaces with Underscores in Python | bobbyhadz
You can also use a for loop to replace the spaces in a string with underscores. ... Copied!my_str = 'bobby hadz com' new_str = '' for char in my_str: if char == ' ': new_str += '_' else: new_str += char print(new_str) # 👉️ bobby_hadz_com ...
🌐
Reddit
reddit.com › r/learnpython › how to replace underscores with letters?
r/learnpython on Reddit: How to replace underscores with letters?
July 1, 2020 - ... If you keep track of the guesses ... = ''.join( c if c in guesses else '_' for c in secret ) # hide = "han__an" ... Split the string into a list. Then replace the character of under at the index you want....
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to replace spaces in a python string with a specific character
5 Best Ways to Replace Spaces in a Python String with a Specific Character - Be on the Right Side of Change
February 26, 2024 - This code snippet takes a string text and uses the replace() method to substitute all spaces with underscores, assigning the result to new_text. The replace() function is simple and efficient for this purpose. List comprehension offers a compact way to iterate over a string’s characters, allowing conditions to be checked for each character.
Find elsewhere
🌐
GitHub
gist.github.com › adriantorrie › 5d886f0a7518104872594effc26f2bbf
Python: Replace non-alphanumeric characters with an underscore · GitHub
Python: Replace non-alphanumeric characters with an underscore · Raw · clean_str.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-replace-whitespace-with-underscore-in-pythonexample.html
How to Replace Whitespace with Underscore in Python? - ItSolutionstuff.com
October 30, 2023 - # Declare String Variable myString = "Hello, This is ItSolutionStuff.com, This is awesome." # Python string replace whitespace with underscore myString = myString.replace(" ", "_") print(myString)
🌐
w3resource
w3resource.com › python-exercises › re › python-re-exercise-23.php
Python: Replace whitespaces with an underscore and vice versa - w3resource
Write a Python program to replace all spaces in a string with underscores and then convert underscores back to spaces.
🌐
Finxter
blog.finxter.com › how-to-replace-whitespaces-with-underscores
How to Replace Whitespaces with Underscores – Be on the Right Side of Change
This method uses Python’s built-in string library and calls the replace() function to replace each whitespace with the underscore character.
🌐
GitHub
github.com › python-poetry › poetry › issues › 7123
Replace special chars with underscores for environment variables with credentials · Issue #7123 · python-poetry/poetry
November 30, 2022 - However, this does not work: poetry.config.config:230 only replaces -s with _s, so it does not match these variables. This request is to support other special characters in the repo name (e.g.
Author   python-poetry
🌐
Python Guides
pythonguides.com › replace-whitespaces-with-underscore-in-python
Python Replace Whitespaces With Underscore
August 25, 2025 - If you’re just starting with Python, you might want to see how this can be done manually with a loop. Although I don’t usually use this in production, it’s a good way to understand how string manipulation works. ... # Example: Replace spaces with underscores using a for loop text = "Chicago Population Growth Report" result = "" for char in text: if char == " ": result += "_" else: result += char print("Original:", text) print("Modified:", result)
🌐
Reddit
reddit.com › r/regex › help replacing spaces with underscores and limiting the amount of underscores in fibery
r/regex on Reddit: Help replacing spaces with underscores and limiting the amount of underscores in Fibery
July 24, 2024 -

I'm using Fibery to manage a bunch of business processes and trying to build a formula that uses their ReplaceRegex function, but struggling to achieve what I want.

ChatGTP keeps giving me solutions that don’t seem to work in Fibery’s approved RegEx format. I'm not entirely sure what they accept but they do link to this page in their documentation: https://medium.com/tech-tajawal/regular-expressions-the-last-guide-6800283ac034

If the input was:

Hello. I'm "___BOB___"! I'm feeling happy / healthy

I want the output to be:

hello_im_bob_im_feeling_happy_healthy

So basically:

  • All spaces should be replaced with underscores

  • All special characters (except for underscores) should be removed

  • There should never be more than 1 underscore in a row in the final output

I’ve got it mostly working with the following

Lower(
ReplaceRegex(
ReplaceRegex(
"Hello.  I'm "___BOB___"! I'm feeling happy / healthy", "[\s_]+", "_"),
"[^a-zA-Z0-9_]", "")
)

but it still spits out the following (based on my example):

hello_im__bob__im_feeling_happy__healthy

As you can see there’s a few spots that have double underscores.

How can I ensure the final output doesn’t have more than 1 underscore in a row? I know there's probably no Fibery experts here, but figured it was worth a shot...appreciate any help that could be provided.

🌐
Better Programming
betterprogramming.pub › 5-different-ways-to-remove-specific-characters-from-a-string-in-python-b0e081839ab9
Better Programming
November 2, 2020 - By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices
Top answer
1 of 3
3

Don't parse Ls. That seems to work well, but for complex cases it may fail.

With Bash's parameter expansion:

for f in *.mp3; do mv -- "$f" "${f//[!0-9a-zA-Z.-]/_}"; done

With Rename:

rename -- 's/[^0-9a-zA-Z.-]/_/g' *.mp3

If you only have standard POSIX tools and assuming no newline characters in the file names,

for f in *.mp3; do 
    mv -- "$f" "$(printf '%s\n' "$f" | sed 's/[^0-9a-zA-Z.-]/_/g')"
done
2 of 3
0

With zsh:

autoload -Uz zmv # best in ~/.zshrc
zmv '(*).mp3' '${f//[^0-9a-zA-Z_.-]/_}'

In zsh, ranges are based on codepoint value, so 0-9 includes 0123456789 only (and not 0123456789٠١٢٣٤٥٦٧٨۰۱۲۳۴۵۶۷۸߀߁߂߃߄߅߆߇߈०१२३४५६७८০১২৩৪৫৬৭৮੦੧੨੩੪੫੬੭੮૦૧૨૩૪૫૬૭૮୦୧୨୩୪୫୬୭୮௦௧௨௩௪௫௬௭௮౦౧౨౩౪౫౬౭౮౸౹౺౻౼౽౾೦೧೨೩೪೫೬೭೮൦൧൨൩൪൫൬൭൮෦෧෨෩෪෫෬෭෮๐๑๒๓๔๕๖๗๘໐໑໒໓໔໕໖໗໘༠༡༢༣༤༥༦༧༨༪༫༬༭༮༯༰༱༳၀၁၂၃၄၅၆၇၈႐႑႒႓႔႕႖႗႘፩፪፫፬፭፮፯፰០១២៣៤៥៦៧៨៰៱៲៳៴៵៶៷៸᠐᠑᠒᠓᠔᠕᠖᠗᠘᥆᥇᥈᥉᥊᥋᥌᥍᥎᧐᧑᧒᧓᧔᧕᧖᧗᧘᧚᪀᪁᪂᪃᪄᪅᪆᪇᪈᪐᪑᪒᪓᪔᪕᪖᪗᪘᭐᭑᭒᭓᭔᭕᭖᭗᭘᮰᮱᮲᮳᮴᮵᮶᮷᮸᱀᱁᱂᱃᱄᱅᱆᱇᱈᱐᱑᱒᱓᱔᱕᱖᱗᱘⁰⁴⁵⁶⁷⁸₀₁₂₃₄₅₆₇₈⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞⅟ↅ↉①②③④⑤⑥⑦⑧⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳⑴⑵⑶⑷⑸⑹⑺⑻⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇⒈⒉⒊⒋⒌⒍⒎⒏⒑⒒⒓⒔⒕⒖⒗⒘⒙⒚⒛⓪⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴⓵⓶⓷⓸⓹⓺⓻⓼⓾⓿❶❷❸❹❺❻❼❽❿➀➁➂➃➄➅➆➇➉➊➋➌➍➎➏➐➑➓〇〡〢〣〤〥〦〧〨㉈㉉㉊㉋㉌㉍㉎㉏㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿㋀㋁㋂㋃㋄㋅㋆㋇㋉㋊㋋㍘㍙㍚㍛㍜㍝㍞㍟㍠㍢㍣㍤㍥㍦㍧㍨㍩㍪㍫㍬㍭㍮㍯㍰㏠㏡㏢㏣㏤㏥㏦㏧㏩㏪㏫㏬㏭㏮㏯㏰㏱㏲㏳㏴㏵㏶㏷㏸㏹㏺㏻㏼㏽㏾꘠꘡꘢꘣꘤꘥꘦꘧꘨꣐꣑꣒꣓꣔꣕꣖꣗꣘꤀꤁꤂꤃꤄꤅꤆꤇꤈꧐꧑꧒꧓꧔꧕꧖꧗꧘꧰꧱꧲꧳꧴꧵꧶꧷꧸꩐꩑꩒꩓꩔꩕꩖꩗꩘꯰꯱꯲꯳꯴꯵꯶꯷꯸012345678𐄇𐄈𐄉𐄊𐄋𐄌𐄍𐄎𐅂𐅃𐅈𐅏𐅘𐅙𐅚𐅛𐅜𐅝𐅞𐅟𐅳𐆊𐋡𐋢𐋣𐋤𐋥𐋦𐋧𐋨𐌠𐌡𐏑𐏒𐒠𐒡𐒢𐒣𐒤𐒥𐒦𐒧𐒨𐡘𐡙𐡚𐡹𐡺𐡻𐡼𐡽𐢧𐢨𐢩𐢪𐢫𐢬𐣻𐣼𐤖𐤚𐤛𐧀𐧁𐧂𐧃𐧄𐧅𐧆𐧇𐩀𐩁𐩂𐩃𐩽𐪝𐫫𐫬𐭘𐭙𐭚𐭛𐭸𐭹𐭺𐭻𐮩𐮪𐮫𐮬𐳺𐳻𐹠𐹡𐹢𐹣𐹤𐹥𐹦𐹧𑁒𑁓𑁔𑁕𑁖𑁗𑁘𑁙𑁦𑁧𑁨𑁩𑁪𑁫𑁬𑁭𑁮𑃰𑃱𑃲𑃳𑃴𑃵𑃶𑃷𑃸𑄶𑄷𑄸𑄹𑄺𑄻𑄼𑄽𑄾𑇐𑇑𑇒𑇓𑇔𑇕𑇖𑇗𑇘𑇡𑇢𑇣𑇤𑇥𑇦𑇧𑇨𑋰𑋱𑋲𑋳𑋴𑋵𑋶𑋷𑋸𑑐𑑑𑑒𑑓𑑔𑑕𑑖𑑗𑑘𑓐𑓑𑓒𑓓𑓔𑓕𑓖𑓗𑓘𑙐𑙑𑙒𑙓𑙔𑙕𑙖𑙗𑙘𑛀𑛁𑛂𑛃𑛄𑛅𑛆𑛇𑛈𑜰𑜱𑜲𑜳𑜴𑜵𑜶𑜷𑜸𑣠𑣡𑣢𑣣𑣤𑣥𑣦𑣧𑣨𑱐𑱑𑱒𑱓𑱔𑱕𑱖𑱗𑱘𑱚𑱛𑱜𑱝𑱞𑱟𑱠𑱡𒐀𒐁𒐂𒐃𒐄𒐅𒐆𒐈𒐉𒐊𒐋𒐌𒐍𒐏𒐐𒐑𒐒𒐓𒐕𒐖𒐗𒐘𒐙𒐚𒐛𒐜𒐞𒐟𒐠𒐡𒐢𒐣𒐤𒐥𒐦𒐧𒐨𒐩𒐪𒐬𒐭𒐮𒐯𒐰𒐱𒐴𒐵𒐶𒐷𒐸𒐹𒐺𒐻𒐼𒐽𒐾𒐿𒑀𒑁𒑂𒑃𒑄𒑅𒑊𒑋𒑌𒑍𒑎𒑏𒑐𒑑𒑒𒑓𒑔𒑕𒑖𒑗𒑘𒑙𒑩𒑪𒑫𒑬𒑭𖩠𖩡𖩢𖩣𖩤𖩥𖩦𖩧𖩨𖭐𖭑𖭒𖭓𖭔𖭕𖭖𖭗𖭘𝍠𝍡𝍢𝍣𝍤𝍥𝍦𝍧𝟎𝟏𝟐𝟑𝟒𝟓𝟔𝟕𝟖𝟘𝟙𝟚𝟛𝟜𝟝𝟞𝟟𝟠𝟢𝟣𝟤𝟥𝟦𝟧𝟨𝟩𝟪𝟬𝟭𝟮𝟯𝟰𝟱𝟲𝟳𝟴𝟶𝟷𝟸𝟹𝟺𝟻𝟼𝟽𝟾𞣇𞣈𞣉𞣊𞣋𞣌𞣍𞣎𞥐𞥑𞥒𞥓𞥔𞥕𞥖𞥗𞥘🄀🄁🄂🄃🄄🄅🄆🄇🄈🄉🄋🄌🆛🆜🆝🆞🆟🆠🆡🆢🆣🆤 like in bash in most modern locales) and a-z only abcdefghijklmnopqrstuvwxyz (not the thousands of characters other shells would match), but that still done on a per character basis (contrary to what would happened if you switched to the C locale in bash to work around the previous issue where it would work on bytes and break up characters into the bytes constituting there encoding for multibyte ones).

It also doesn't choke on bytes not forming valid characters in the locale (and those bytes would match [^0-9a-zA-Z_.-]), so in the end, the resulting files should only contain characters among 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.-, and no non-character.

In bash, you could do something approaching with:

accepted_characters='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.-'
for f in *[^$accepted_characters]*.mp3; do
  mv -i -- "$f" "${f//[^$accepted_characters]/_}"
done

(without the safeguards of zmv).

🌐
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. str.isalnum() method checks if a character is alphanumeric (letters ...
🌐
DEV Community
dev.to › bugsanalyze › how-to-replace-whitespaces-in-a-string-with-underscore-without-using-replace-function-in-python-5gab
How to replace whitespaces in A String with underscore without using replace function in python? - DEV Community
August 9, 2022 - In this blog we will discuss on how to replace all occurrences of whitespaces with underscore in a string in python . I have a string as below. ... If found empty space at particular index replace the character by dividing into substrings and appending underscore