From the docs:
str.split([sep[, maxsplit]])Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most
maxsplit+1elements).
s.split('mango', 1)[1]
Answer from Ignacio Vazquez-Abrams on Stack OverflowFrom the docs:
str.split([sep[, maxsplit]])Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most
maxsplit+1elements).
s.split('mango', 1)[1]
>>> s = "123mango abcd mango kiwi peach"
>>> s.split("mango", 1)
['123', ' abcd mango kiwi peach']
>>> s.split("mango", 1)[1]
' abcd mango kiwi peach'
I want to split a string by a character on its first occurence, which belongs to a list of characters. How to do this in python? - Stack Overflow
arrays - How can I split a string at the first occurrence of a letter in Python? - Stack Overflow
split - Python - Cut string based on first occurrence of characters in list - Stack Overflow
python - How can i split a string into two on the first occurrence of a character - Stack Overflow
Using re.search:
import re
strs = ["71 1 * abwhf", "8 askg", "*14 snbsb", "00ab"]
def split_on_letter(s):
match = re.compile("[^\W\d]").search(s)
return [s[:match.start()], s[match.start():]]
for s in strs:
print split_on_letter(s)
The regex [^\W\d] matches all alphabetical characters.
\W matches all non-alphanumeric characters and \d matches all numeric characters. ^ at the beginning of the set inverts the selection to match everything that is not (non-alphanumeric or numeric), which corresponds to all letters.
match searches the string to find the index of the first occurrence of the matching expression. You can slice the original string based on the location of the match to get two lists.
The only way I can think of is to write the function yourself:
import string
def split_letters(old_string):
index = -1
for i, char in enumerate(old_string):
if char in string.letters:
index = i
break
else:
raise ValueError("No letters found") # or return old_string
return [old_string[:index], old_string[index:]]
I am currently making a project for university and I'm having a problem making a code for the method I'm finding.
I am finding a method to remove a specified character sc from a string. But for strings that contain more than 1 occurrence of the sc, only one occurrence of the sc should be removed from the right.
Example:
before_string = "abcdefaghsjsajs"
sc = "a"
after_string = "abcdefaghsjsjs"
I have tried using the replace() method. It works very well with my code, especially because I can tell it to only remove 1 occurrence of the sc. However, it doesn't work when there is more than 1 occurrence of the sc because it removes the sc from the left and not from the right.
Does anyone have any idea? Thank you.
This is what str.partition is for:
>>> 'jvm.args= -Dappdynamics.com=true, -Dsomeotherparam=false,'.partition('=')
('jvm.args', '=', ' -Dappdynamics.com=true, -Dsomeotherparam=false,')
From the docs:
str.partition(sep)
Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.
New in version 2.5.
From the split documentation
str.split([sep[, maxsplit]])
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements)
>>> 'jvm.args= -Dappdynamics.com=true, -Dsomeotherparam=false,'.split('=',1)
['jvm.args', ' -Dappdynamics.com=true, -Dsomeotherparam=false,']