Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Saturday, November 14, 2015

String contains in python

s = "A nice string search 234"
res = ""
items = ['a', '4', '6']
for x in items:
#print x
if s.find(x) == -1:
res = x + " is not in given string"
print  res
else:
res = x + " is in given string"
print res

Wednesday, June 17, 2015

Remove punctation from the text - Python


import re
import string
def removePunctuation(text):
   
    #converts to lowercase
    text1 = text.lower()
   
    #removes punctuation
    #punctation = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    text2 =  re.sub('[%s]' % string.punctuation, ' ' ,text1)

    #removes leading and trailing spaces
    text3 = text2.strip()
    return text3


   

print removePunctuation("'The best investments today,'")
print removePunctuation("Misery in Paradise;")

Output:

the best investments today
misery in paradise