You can use str.find method with a simple indexing :
>>> s="have an egg please"
>>> s[s.find('egg'):]
'egg please'
Note that str.find will returns -1 if it doesn't find the sub string and will returns the last character of your string.So if you are not sure that always your string is contain the sub string you better to check the value of str.find before using it.
>>> def slicer(my_str,sub):
... index=my_str.find(sub)
... if index !=-1 :
... return my_str[index:]
... else :
... raise Exception('Sub string not found!')
...
>>>
>>> slicer(s,'egg')
'egg please'
>>> slicer(s,'apple')
Sub string not found!
Answer from Kasravnd on Stack OverflowYou can use str.find method with a simple indexing :
>>> s="have an egg please"
>>> s[s.find('egg'):]
'egg please'
Note that str.find will returns -1 if it doesn't find the sub string and will returns the last character of your string.So if you are not sure that always your string is contain the sub string you better to check the value of str.find before using it.
>>> def slicer(my_str,sub):
... index=my_str.find(sub)
... if index !=-1 :
... return my_str[index:]
... else :
... raise Exception('Sub string not found!')
...
>>>
>>> slicer(s,'egg')
'egg please'
>>> slicer(s,'apple')
Sub string not found!
string = 'Stack Overflow'
index = string.find('Over') #stores the index of a substring or char
string[:index] #returns the chars before the seen char or substring
Hence, the output will be
'Stack '
and
string[index:]
will give
'Overflow'
string - How to remove all characters before a specific character in Python? - Stack Overflow
replace - How to remove all characters after a specific character in python? - Stack Overflow
Removing a all specific text after a word in Python.
How to remove part of string after certain character in python?
There are a few ways. Here are a few off the top of my head:
>>> s = "abcd//efgh"
>>> s.find("/")
4
>>> s[:s.find("/")]
'abcd'
>>> s.split("/")
['abcd', '', 'efgh']
>>> s.split("/", maxsplit=1)
['abcd', '/efgh']
>>> s.split("/", maxsplit=1)[0]
'abcd'
>>> import re
>>> re.sub("/.*$", "", s)
'abcd'
The last is overkill here and I wouldn't use it, but regexs are often appropriate for doing search & replace operations. Either of the first two would work pretty well. The first depends on the search string appearing though. Otherwise, s.find will return -1 and then s[:-1] will lop off the last character:
>>> s = "abcdef"
>>> s[:s.find("/")]
'abcde' More on reddit.com Split on your separator at most once, and take the first piece:
sep = '...'
stripped = text.split(sep, 1)[0]
You didn't say what should happen if the separator isn't present. Both this and Alex's solution will return the entire string in that case.
Assuming your separator is '...', but it can be any string.
text = 'some string... this part will be removed.'
head, sep, tail = text.partition('...')
>>> print head
some string
If the separator is not found, head will contain all of the original string.
The partition function was added in Python 2.5.
S.partition(sep)->(head, sep, tail)Searches for the separator sep in S, and returns the part before it, the separator itself, and the part after it. If the separator is not found, returns S and two empty strings.
Hello,
I want to remove the entire substring after a particular word in Python.
For instance, if the word is "Excerpt," all text after the word would be removed.
However, my code is not accomplishing that task.
If the word exists in the string, I would find the index of it.
After that, I would subtract the len(text)-index of the word, which should point to the word.
Am I not returning from 0-index of the word, hence removing the word, and everything after the word.
What is the issue with my code?
str1 = 'Excerpt'
if str1 in text.split():
strLength = text.find(str1)
a = text[0:len(text)-strLength]
print(a)
If I had a string like "1234///5678" and I wanted to remove everything after the first slash, how would I go about it? Thank You in advance.
There are a few ways. Here are a few off the top of my head:
>>> s = "abcd//efgh"
>>> s.find("/")
4
>>> s[:s.find("/")]
'abcd'
>>> s.split("/")
['abcd', '', 'efgh']
>>> s.split("/", maxsplit=1)
['abcd', '/efgh']
>>> s.split("/", maxsplit=1)[0]
'abcd'
>>> import re
>>> re.sub("/.*$", "", s)
'abcd'
The last is overkill here and I wouldn't use it, but regexs are often appropriate for doing search & replace operations. Either of the first two would work pretty well. The first depends on the search string appearing though. Otherwise, s.find will return -1 and then s[:-1] will lop off the last character:
>>> s = "abcdef"
>>> s[:s.find("/")]
'abcde'
+u/CompileBot python
s = "1234///5678"
result = s[:s.find("/") + 1]
print(result)
string1 = 'bla/bla1/blabla/bla2/bla/bla/wowblawow1'
string2 = string1.split(r'/')[-1] # Out[2]: 'wowblawow1'
see https://docs.python.org/2/library/stdtypes.html#str.split to see how it works. But as @Emilien suggested, if are looking for extracting basename, use os.path: https://docs.python.org/2/library/os.path.html
Or maybe you are even looking for this?
>>> import os
>>> os.path.basename("/var/log/syslog")
'syslog'
>>> os.path.dirname("/var/log/syslog")
'/var/log'