How can I replace multiple symbols with the method replace()? Is it possible to do that with just one replace()? Or is there any better ways?
The symbols can look like this for example -,+,/,',.,&.
You can use re.sub and put the characters in a character class:
import re
re.sub('[-+/\'.&]', replace_with, input)
You may do it using str.join with generator expression (without importing any library) as:
>>> symbols = '/-+*'
>>> replacewith = '.'
>>> my_text = '3 / 2 - 4 + 6 * 9' # input string
# replace char in string if symbol v
>>> ''.join(replacewith if c in symbols else c for c in my_text)
'3 . 2 . 4 . 6 . 9' # Output string with symbols replaced
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With