0

This is the code I have at the moment:

UserSentence = input('Enter your chosen sentence: ')
UserSentence = UserSentence.split()
print(UserSentence)

say the UserSentence was 'life is short, stunt it', how would I remove the comma after .split()? if possible.

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
K.Ruddick
  • 11
  • 4

1 Answers1

3

replace before you split:

In [4]:  'life is short, stunt it'.replace(',',' ').split()
Out[4]: ['life', 'is', 'short', 'stunt', 'it']

If you want to remove all punctuation, you can use str.translate to replace any punctuation with a space and then split:

s = 'life is short, stunt it!!?'

from string import punctuation

tbl = str.maketrans({ord(ch):" " for ch in punctuation})


print(s.translate(tbl).split())

Output:

['life', 'is', 'short', 'stunt', 'it']
Padraic Cunningham
  • 160,756
  • 20
  • 201
  • 286
  • thanks very much. i have a few questions: 1. what does the from string import punctuation do? 2. what does tbl = str.maketrans({ord(ch):" " for ch in punctuation}) do? – K.Ruddick Nov 30 '15 at 12:13