Megha Aggarwal
Megha Aggarwal

Reputation: 101

Remove unnecessary character from string in python

zenPython = ''' The Zen of Python, by Tim Peters

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. '''

How to remove unnecessary character like --,*,! in this zenpython string using list comprehension and split??

I have made solution using replace, and normal looping in python but I need an optimal solution for this.

Upvotes: 1

Views: 1085

Answers (4)

Prakash B Jain
Prakash B Jain

Reputation: 11

Special characters removed with strip() & list comprehension

words = zenPython.split()
for i in [',', '.', '-', '*', '!']:
    words = [x.strip(i) for x in words]    #list of words with ", . - * !" characters removed

Upvotes: 1

kederrac
kederrac

Reputation: 17322

you can use a regular expression :

import re

# note that * is a special character in regular expressions so you have to use \*
unwanted_char = '--,\*,!'  
pattern = '|'.join(unwanted_char.split(','))
print(re.sub(pattern , '', zenPython))

output:

The Zen of Python, by Tim Peters

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one and preferably only one obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea.

Upvotes: 0

Hades1598
Hades1598

Reputation: 320

You can make use of regular expressions to do this for you.

import re

output = re.sub('[^A-Z a-z 0-9 \n]+', '', my_string)

print(output)

This should remove the unnecessary characters you mentioned in the question.

Hope this helps!

Upvotes: 0

vikAy
vikAy

Reputation: 324

Use the maketrans() function where ur first parameter is list of "--, *,!" and second list constisting of empty strings - " "

Upvotes: 0

Related Questions