Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need some help with python string / slicing operations

This is a very newbie question and i will probably get downvoted for it, but i quite honestly couldn't find the answer after at least an hour googling. I learned how to slice strings based on "exact locations" where you have to know exactly where the word ends. But i did not find any article that explained how do it on "non static" strings that could change.

Also i do not want to use string.split() in this case as its a little overkill for what i need.

I basically have a string like this:

myString = "!save python Python is a high-level object oriented language created by Guido van Rossum."
# the format is !save [singleword] [definition]

i need to "slice" this string but i cant figure out a proper way to do it.

i need to save a to variable the title (python in this case) and the definition of this string. Somethig like:

title = myString[1]
definition = myString[everything after string[1]

I'm not exactly sure how to do this when you have a dynamic string where you dont know where each word ends.

I would greatly appreciate some pointers on what functions/methods should i read on to archieve this. Thank you in advance.


1 Answers

Why is split overkill?

verb, title, definition = myString.split (' ', 2)
like image 193
Joe Holloway Avatar answered Jan 22 '26 17:01

Joe Holloway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!