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'
Split a string only by first space in python - Stack Overflow
Get+split split a string and get the first element
What is the "best" way to get the first word in sentence?
Using Split only on the first occurrence of a character?
So there are a million StackOverflow posts on this topic but I couldn't find the one for my exact use case and concerns. I'm trying to create a reddit bot which gets summoned through a trigger word, which should be the first word in a comment.
For example this sentence:
str1 = "trigger123! Lorem ipsum dolor sit amet"
the trigger (which is the first word) is the string "trigger123!".
Now I know you can split a string by spaces into a list of words using:
str1.split()[0] # returns 'trigger123!'
My only concern (which is probably premature optimization) is what if the string is very large, like 1000 words or something, and I call split only to just use the first word. With modern computers this probably isn't an issue but I'm wondering if there's a better way.
Like is there a built-in function to ".split()" only the first N words in a string? I know I could also write my own function to detect the first space and store that, but just wanted to see if there exists something already.
What about simply checking the first N characters and seeing if it matches my trigger? Like "trigger123!" is 11 characters so what if I always check the first 11 characters to see if it matches? Is that better than the split method?
I feel like I'm overthinking this lol