-1

I have like 3 strings and how can I remove the punctuation and make all the reviews lower-case and then print out all 3 reviews thereafter.

Review1 = 'My great auntie has lived at Everton Park for decades, and once upon a time I even lived here too, and I remember the days before when there was nothing remotely hipster about this housing block.  It is really cool to see cute new cafes and coffee shops moving in, and I've been to Nylon every time I'm back in town.'

Review2 = 'Solid coffee in the Outram Park neighborhood. Location is hidden in a HDB block so you definitely need to search for it. Minus one star for limited seating options'

Review3 = 'Deserve it, truly deserves this much reviews. I will describe coffee here as honest, sincere, decent, strong, smart.'

5 Answers5

0
Review1 = "My great auntie has lived at Everton Park for decades, and once upon a time I even lived here too, and I remember the days before when there was nothing remotely hipster about this housing block.  It is really cool to see cute new cafes and coffee shops moving in, and I've been to Nylon every time I'm back in town."

import string
Review1_Fixed = Review1.lower().translate(str.maketrans('', '', string.punctuation))
print(Review1_Fixed)

Output:

"my great auntie has lived at everton park for decades and once upon a time i even lived here too and i remember the days before when there was nothing remotely hipster about this housing block  it is really cool to see cute new cafes and coffee shops moving in and ive been to nylon every time im back in town"

For more information on what this command is doing, or more ways of doing this see this post.

Jmonsky
  • 1,519
  • 1
  • 9
  • 16
  • Can i come up with a function so i could use it at one time to apply the function to all 3 strings. I have an issue coding out the function that can remove punctuation and ensure my text is in lower case – Chua S Yang Jul 17 '19 at 15:30
  • Sure. `Fixtext = lambda t : t.lower().translate(str.maketrans('', '', string.punctuation))` Then just say `Review1_Fixed = Fixtext(Review1)` – Jmonsky Jul 17 '19 at 15:33
0

Using re module:

Review1 = '''My great auntie has lived at Everton Park for decades, and once upon a time I even lived here too, and I remember the days before when there was nothing remotely hipster about this housing block. It is really cool to see cute new cafes and coffee shops moving in, and I've been to Nylon every time I'm back in town.'''
Review2 = '''Solid coffee in the Outram Park neighborhood. Location is hidden in a HDB block so you definitely need to search for it. Minus one star for limited seating options'''
Review3 = '''Deserve it, truly deserves this much reviews. I will describe coffee here as honest, sincere, decent, strong, smart.'''

import re

def strip_punctuation_make_lowercase(*strings):
    return map(lambda s: re.sub(r'[^\s\w]+', '', s).lower(), strings)

Review1, Review2, Review3 = strip_punctuation_make_lowercase(Review1, Review2, Review3)

print(Review1)
print()

print(Review2)
print()

print(Review3)
print()

Prints:

my great auntie has lived at everton park for decades and once upon a time i even lived here too and i remember the days before when there was nothing remotely hipster about this housing block it is really cool to see cute new cafes and coffee shops moving in and ive been to nylon every time im back in town

solid coffee in the outram park neighborhood location is hidden in a hdb block so you definitely need to search for it minus one star for limited seating options

deserve it truly deserves this much reviews i will describe coffee here as honest sincere decent strong smart
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0
In [23]: whitelist = set(string.ascii_letters)                                                                                                                                                                                                                                                                                

In [24]: rev1 = "My great auntie has lived at Everton Park for decades, and once upon a time I even lived here too, and I remember the days before when there was nothing remotely hipster about this housing block. It is really cool to see cute new cafes and coffee shops moving in, and I've been to Nylon every time I'm
    ...:  back in town."                                                                                                                                                                                                                                                                                                      

In [25]: ''.join([char for char in rev1 if char in whitelist])                                                                                                                                                                                                                                                                
Out[25]: 'MygreatauntiehaslivedatEvertonParkfordecadesandonceuponatimeIevenlivedheretooandIrememberthedaysbeforewhentherewasnothingremotelyhipsteraboutthishousingblockItisreallycooltoseecutenewcafesandcoffeeshopsmovinginandIvebeentoNyloneverytimeImbackintown'

In [26]: whitelist = set(string.ascii_letters + ' ')                                                                                                                                                                                                                                                                          

In [27]: ''.join([char for char in rev1 if char in whitelist])                                                                                                                                                                                                                                                                
Out[27]: 'My great auntie has lived at Everton Park for decades and once upon a time I even lived here too and I remember the days before when there was nothing remotely hipster about this housing block It is really cool to see cute new cafes and coffee shops moving in and Ive been to Nylon every time Im back in town'
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
0

__contains__ method defines how instances of class behave when they appear at right side of in and not in operator.

from string import ascii_letters

Review1 = "My great auntie has lived at Everton Park for decades, and once upon a time I even lived here too, and I remember the days before when there was nothing remotely hipster about this housing block.  It is really cool to see cute new cafes and coffee shops moving in, and I've been to Nylon every time I'm back in town."

key = set(ascii_letters + ' ') # key = set('abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ')
Review1_ = ''.join(filter(key.__contains__, Review1)).lower()
print (Review1_)

output:

my great auntie has lived at everton park for decades and once upon a time i even lived here too and i remember the days before when there was nothing remotely hipster about this housing block  it is really cool to see cute new cafes and coffee shops moving in and ive been to nylon every time im back in town
ncica
  • 7,015
  • 1
  • 15
  • 37
0

For remove the punctuation s.translate(None, string.punctuation) or create you own function def Punctuation(string):
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

for x in string.lower(): 
    if x in punctuations: 
        string = string.replace(x, "") 

# Print string without punctuation 
print(string) 

For lower case string.lower()