Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split Arguments ?
#1
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.
Reply
#2
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]
ndc85430 likes this post
Reply
#3
I see it now. Thanks much!
Reply
#4
FYI:
Split documentation here:
reverse split str.rsplit
forward split: str.split
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class takes no arguments bily071 2 649 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 3,885 Mar-02-2017, 10:23 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020