Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Interpreting Python Code
#1
Hi,

I have this code:
clientNo = ClientNumber.split("ClientNo", 1)[1][0:50]
I don't know what does the last [0:50] mean, please help.
From the beginning I understand that we say
Quote:split the string every time you see the word 'ClientNo', however only do it once
so it will return a list with 2 index.
Then we further say,
Quote:actually give me the second index, leave the first one
That's how far I can go.
Reply
#2
Let's start with:

clientNo = ClientNumber.split("ClientNo", 1)
That takes the string ClientNumber, and splits it everywhere there is the sub-string 'ClientNo', but only does it once (because of the final one). So if ClientNumber is '123 ClientNo 456 ClientNo 789', the result will be ['123', '456 ClientNo 789']. Note that the result is a list.

Then we have:

clientNo = ClientNumber.split("ClientNo", 1)[1]
This indexes the list we got from split. Since Python is 0-indexed, the index 1 refers to the second item in the list. In the above example this would give us '456 ClientNo 789'.

Finally we have:

clientNo = ClientNumber.split("ClientNo", 1)[1][0:50]
The [0:50] slices the string we got from the [1]. Especially for slices, it is helpful to think of the indexes as existing between the characters of the string, like this:

Output:
characters: 4 5 6 C l i e n t ... indexes: 0 1 2 3 4 5 6 7 8 9 10 ...
So we can see that [0:5] will get '456 C', the first 5 characters. Therefore [0:50] will get the first 50 characters. Note that 0 is the default first index in a slice, so [0:50] is often written as [:50].

So altogether, clientNo = ClientNumber.split("ClientNo", 1)[1][0:50] gets the first 50 characters of text after the first instance of 'ClientNo'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you, I'll go learn slicing, now I know what that is.

(Apr-29-2019, 11:39 AM)ichabod801 Wrote: Let's start with:

clientNo = ClientNumber.split("ClientNo", 1)
That takes the string ClientNumber, and splits it everywhere there is the sub-string 'ClientNo', but only does it once (because of the final one). So if ClientNumber is '123 ClientNo 456 ClientNo 789', the result will be ['123', '456 ClientNo 789']. Note that the result is a list.

Then we have:

clientNo = ClientNumber.split("ClientNo", 1)[1]
This indexes the list we got from split. Since Python is 0-indexed, the index 1 refers to the second item in the list. In the above example this would give us '456 ClientNo 789'.

Finally we have:

clientNo = ClientNumber.split("ClientNo", 1)[1][0:50]
The [0:50] slices the string we got from the [1]. Especially for slices, it is helpful to think of the indexes as existing between the characters of the string, like this:

Output:
characters: 4 5 6 C l i e n t ... indexes: 0 1 2 3 4 5 6 7 8 9 10 ...
So we can see that [0:5] will get '456 C', the first 5 characters. Therefore [0:50] will get the first 50 characters. Note that 0 is the default first index in a slice, so [0:50] is often written as [:50].

So altogether, clientNo = ClientNumber.split("ClientNo", 1)[1][0:50] gets the first 50 characters of text after the first instance of 'ClientNo'.

Last question, if the string has 500 character, would it split it in 50 batches? or is would only take the fist 50 and ignore the rest?
Reply
#4
(Apr-29-2019, 12:48 PM)NewBeie Wrote: Last question, if the string has 500 character, would it split it in 50 batches? or is would only take the fist 50 and ignore the rest?

It would only take the first 50. If you wanted to get batches that were 50 characters long, you would need to loop the slicing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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