Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:]
is always equal to s
:
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'
I don't understand what thee above notation is.
Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:]
is always equal to s
:
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'
I don't understand what thee above notation is.
The python indices are as below
P y t h o n
0 1 2 3 4 5
So if you ask s[0:1]
it is start index of 0
to end index of 1
. Thus only P
So if you ask s[4:]
, anything right of index at 4(start).
So if you ask s[:4]
, anything left of index at 4(end).
If the word is Python
This means each letters index will be as follows and you can select each letter by this method
P = word[0]
y = word[1]
t = word[2]
h = word[3]
o = word[4]
n = word[5]
You can also select a range or letter by using slice notation
. This uses the :
in between the desired range
So...
word[2:4]
This will select the letters in index [2]
and [3]
but will NOT include the last index number [4]
If you leave it blank word[:]
you will return the whole string Python
because it will include everything from the start
to the end
.
If you use word[:2]
it will return Pyt
If you use word[2:]
it wil return thon
note that the last letter is included when the right side is left blank.