Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Interpreting Python Code
#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


Messages In This Thread
Interpreting Python Code - by NewBeie - Apr-29-2019, 11:16 AM
RE: Interpreting Python Code - by ichabod801 - Apr-29-2019, 11:39 AM
RE: Interpreting Python Code - by NewBeie - Apr-29-2019, 12:48 PM
RE: Interpreting Python Code - by ichabod801 - Apr-29-2019, 01:51 PM

Forum Jump:

User Panel Messages

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