sagar suri
sagar suri

Reputation: 4731

How to strip special characters from the start and end of the string in python

I have a list of sentences which consist of special characters(!?@#$.) at the end of the string. I need to strip them. Here is the list of sentences:

['The first time you see The Second Renaissance it may look boring.', 'Look at it at least twice and definitely watch part 2.', 'It will change your view of the matrix.', 'Are the human people the ones who started the war?', 'Is AI a bad thing?']

My output should be like this:

['The first time you see The Second Renaissance it may look boring', 'Look at it at least twice and definitely watch part 2', 'It will change your view of the matrix', 'Are the human people the ones who started the war', 'Is AI a bad thing']

Upvotes: 0

Views: 1855

Answers (3)

Aaditya Ura
Aaditya Ura

Reputation: 12679

You can try translate method :

import unicodedata
import sys

data1=['The first time you see The Second Renaissance it may look boring.', 'Look at it at least twice and definitely watch part 2.', 'It will change your view of the matrix.', 'Are the human people the ones who started the war?', 'Is AI a bad thing?']


data=dict.fromkeys([i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P')])

def remove_punctuation(sentence):
    return sentence.translate(data)

for i in data1:
    print(remove_punctuation(i))

output:

The first time you see The Second Renaissance it may look boring
Look at it at least twice and definitely watch part 2
It will change your view of the matrix
Are the human people the ones who started the war
Is AI a bad thing

Upvotes: 0

Steve Barnes
Steve Barnes

Reputation: 28405

Simply use string.strip with all of the characters you need removed, in a list compression, e.g.:

In [1]: l = ['The first time you see The Second Renaissance it may look boring.', 'Look at it at least twice and definitely watch part 2.', 'It will change
   ...:  your view of the matrix.', 'Are the human people the ones who started the war?', 'Is AI a bad thing?']

In [2]: p = [i.strip('.,?!') for i in l]

In [3]: p
Out[3]:
['The first time you see The Second Renaissance it may look boring',
 'Look at it at least twice and definitely watch part 2',
 'It will change your view of the matrix',
 'Are the human people the ones who started the war',
 'Is AI a bad thing']

In [4]:

Upvotes: 1

Wasi Ahmad
Wasi Ahmad

Reputation: 37761

If you only want to remove characters from the beginning and the end, you could use the string.strip() method.

Example:

strp_chars = '!?@#$.'
sentence = 'The first time you see The Second Renaissance it may look boring.'
print(sentence.strip(strp_chars))

Upvotes: 3

Related Questions