2

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.

3 Answers 3

12

Why is split overkill?

verb, title, definition = myString.split (' ', 2)
Sign up to request clarification or add additional context in comments.

3 Comments

Would this affect the third part of his string that has multiple spaces in it?
Nope, second parameter for split says the maximum number of times to split the string. After the first 2 the rest of the string is returned.
@mandroid The second parameter tells it how many splits to perform. In this case it will stop splitting after it hits the second occurrence of the delimiter. docs.python.org/library/stdtypes.html#str.split When in doubt, crack open a shell and give it a try.
2

If you have spaces between your command, title, and definition you could:

wordList = myString.split()
cmd = wordList[0] # !save
title = wordList[1] # python
definition = ' '.join(wordList[2:])  # Python is a high-level object oriented language created by Guido van Rossum.

If you really would rather not use split you could use regular expressions:

import re
m = re.match('(/S+)/s*(/S+)/s*(.*)')
cmd = m.group(1)
title = m.group(2)
definition = m.group(3)

3 Comments

this is a great answer! thank you so much! i will definitely use some of your suggestions ! thanks!
Ah, you don't want to use a saw. Here is a chainsaw instead!
Note that myString.split() does normalizing of whitespace, so doing the join() again afterwards doesn't get you back to the original "definition".
1

The selected answer (after PEP8ing):

verb, title, definition = my_string.split(' ', 2)

splits on a single space. It's likely a better choice to split on runs of whitespace, just in case there are tabs or multiple spaces on either side of the title:

verb, title, definition = my_string.split(None, 2)

Also consider normalising the whitespace in the definition:

definition = ' '.join(definition.split())

8 Comments

@mhawke: I think that normali[sz]e_whitespace() would be a good candidate for a str method ... the join/split caper is very inefficient, especially when no change is required; a built-in could just return a reference to the original string in that case. One annoying thing about join/split is that \xA0 aka   is not considered whitespace in the (default) C-locale in Python 2.x.
Generally speaking I like your recommendations, but I took his specification literally: # the format is !save [singleword] [definition]
@jholloway: re "I took his specification literally" ... your confession comes rather late. Have you truly repented?
@John Machin Not sure what you mean, but I'll repent for the PEP8 mistake in my solution if that makes you happy :)
@jholloway: I meant: Have you truly repented taking the OP's specification literally?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.