Python Forum
Split Arguments ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Split Arguments ? (/thread-37537.html)



Split Arguments ? - jesse68 - Jun-23-2022

Hi, trying to understand these arguments to this split command which I can't find documented. They may not be related to the split command.

Adding [1::2] to the end of the split. In my example it appears to omit the first and last '[]'s of my string. What exactly is this code, what documentation should I look at to understand it?


mystring = '[ "word1" "word2" "word3" "word4" ]'
print (mystring)
result = mystring.split('"') 
result2 = mystring.split('"')  [1::2]
print ("--")
print (result)
print ("--")
print (result2)
OUPUT:
[ "word1" "word2" "word3" "word4" ]
--
['[ ', 'word1', ' ', 'word2', ' ', 'word3', ' ', 'word4', ' ]']
--
['word1', 'word2', 'word3', 'word4']
>

Thanks so much.


RE: Split Arguments ? - bowlofred - Jun-23-2022

You're correct, this doesn't have anything to do with split() itself. split() returns a list. List and other sequences support "slicing". Documentation about it can be see at Sequence Operations. It has the start, stop, and stride values separated by colons.

The start value of 1 says to return the element from index 1 of the list (skipping index 0), the lack of a stop value says to continue until the end, and the stride value of 2 says to return every second object.

>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][1::2]
[1, 3, 5, 7, 9]



RE: Split Arguments ? - jesse68 - Jun-24-2022

I see it now. Thanks much!


RE: Split Arguments ? - Larz60+ - Jun-24-2022

FYI:
Split documentation here:
reverse split str.rsplit
forward split: str.split