I wish to remove '#' from a client string.
Assume the string is '#Freahlife I just ate an apple'
I wish to write a function and extract only 'I just ate an apple'
How can I achieve this by using python? Thank you!
CodePudding user response:
You can use python's re module.
import re
re.sub(r"#\w \s?", "", "#Freahlife I just ate an apple")
Out[8]: 'I just ate an apple'
CodePudding user response:
If #
is followed by Freahlife
only, you can use replace
function as mentioned by @anosha_rehan. But if the following word is unknown, you can use split
function and put all the words in a list. Then, you can just loop and check if #
is in the current word and has an index of 0 (making sure #
is found at the start of the word). Then, remove that from the list and convert the list into a string.
CodePudding user response:
You can use the replace
function and strip
to remove any whitespace:
'#Freahlife I just ate an apple'.replace('#Freahlife','').strip()
or in generic:
' '.join('#Freahlife I just ate an apple'.split(' ')[1:])