Strip specific words from string

phpstringwordpythonremovecodespecificstrip

Last Update : 2023-09-22 UTC 10:41:54 AM

Answers of > Strip specific words from string

1 w3coded string strip w3coded string strip Got distracted and w3coded string strip didn't finish up with the examples for using re, w3coded string strip which will allow removal of leading/trailing w3coded string strip spaces. w3coded string strip – metatoaster w3coded string strip May 15 '14 at 4:41 w3coded string strip , 2 w3coded string strip w3coded string strip Try print "papa is papa w3coded string strip is papa".replace('papa', '') w3coded string strip – thefourtheye w3coded string strip May 15 '14 at 4:32 w3coded string strip , w3coded string strip 1 w3coded string strip Try print w3coded string strip "papa is papa is papa".replace('papa', '') w3coded string strip – thefourtheye w3coded string strip May 15 '14 at 4:30 w3coded string strip , w3coded string strip Minifig head with orange glasses - w3coded string strip Which set is it from? w3coded string strip

Use str.replace.

>>> papa.replace('papa', '')
' is a good man'
>>> app.replace('papa', '')
'app is important'

Alternatively use re and use regular expressions. This will allow the removal of leading/trailing spaces.

>>> import re
>>> papa = 'papa is a good man'
>>> app = 'app is important'
>>> papa3 = 'papa is a papa, and papa'
>>>
>>> patt = re.compile('(\s*)papa(\s*)')
>>> patt.sub('\\1mama\\2', papa)
'mama is a good man'
>>> patt.sub('\\1mama\\2', papa3)
'mama is a mama, and mama'
>>> patt.sub('', papa3)
'is a, and'

Easiest way would be to simply replace it with an empty string.

s = s.replace('papa', '')

You can also use a regexp with re.sub:

article_title_str = re.sub(r'(\s?-?\|?\s?Times of India|\s?-?\|?\s?the Times of India|\s?-?\|?\s+?Gadgets No'',
                           article_title_str, flags=re.IGNORECASE)

Providing you know the index value of the beginning and end of each word you wish to replace in the character array, and you only wish to replace that particular chunk of data, you could do it like this.

>>> s = "papa is papa is papa"
>>> s = s[:8]+s[8:13].replace("papa", "mama")+s[13:]
>>> print(s)
papa is mama is papa

Alternatively, if you also wish to retain the original data structure, you could store it in a dictionary.

>>> bin = {}
>>> s = "papa is papa is papa"
>>> bin["0"] = s
>>> s = s[:8]+s[8:13].replace("papa", "mama")+s[13:]
>>> print(bin["0"])
papa is papa is papa
>>> print(s)
papa is mama is papa

A bit 'lazy' way to do this is to use startswith- it is easier to understand this rather regexps. However regexps might work faster, I haven't measured.

>>> papa = "papa is a good man"
>>> app = "app is important"
>>> strip_word = 'papa'
>>> papa[len(strip_word):] if papa.startswith(strip_word) else papa
' is a good man'
>>> app[len(strip_word):] if app.startswith(strip_word) else app
'app is important'

Current topics : Strip specific words from string

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 12:28:25 PM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 12:28:07 PM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 12:28:00 PM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 12:27:52 PM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 12:27:33 PM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 12:27:18 PM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 12:27:09 PM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 12:27:02 PM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 12:26:49 PM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 12:26:43 PM

Top
© 2023 W3CODED - All Rights Reserved.