How To Remove Certain Characters From A Variable? (Python)
Let's suppose I have a variable called data. This data variable has all this data and I need to remove certain parts of it while keeping most of it. Let's say I needed to remove al
Solution 1:
Just replace them:
data = data.replace(',', '')
If you have more characters, try using .translate()
:
data = data.translate(None, ',.l?asd')
Solution 2:
def remove_chars(data, chars):
new_data = data
for ch in chars:
new_data = new_data.replace(ch, '')
return new_data
Example:
>>> data = 'i really dont like vowels'
>>> remove_chars(data, 'aeiou')
' rlly dnt lk vwls'
Post a Comment for "How To Remove Certain Characters From A Variable? (Python)"