Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a text without substring in Python

Tags:

python

text

I want to search for a word in the text and then print the text without that word. For example, we have the text "I was with my friend", I want the text be "I with my friend". I have done the following so far:

text=re.compile("[^was]")   
val = "I was with my friend"
if text.search(val): 
    print text.search(val) #in this line it is obvious wrong
else:
    print 'no'
like image 299
Nasser Avatar asked Feb 20 '26 18:02

Nasser


1 Answers

val = "I was with my friend"

print val.replace("was ", "")

Output:

I with my friend
like image 168
Jason Avatar answered Feb 23 '26 06:02

Jason