Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Hi again, in the previous lecture we
talked about strings and in this lecture
3
again we are going deeper into learning
strings and specifically we're going to add
5
indexing. Indexing is a very important
concept not only with strings but
7
with all the data type that we'll be
looking at later such as lists,
9
tuples and dictionaries, so please follow
this lecture. Yeah, let's talk about
11
indexing. Let me open the Python
interactive session. Let me create a
13
string, "Hi there!"
exclamation mark and close quote.
15
Now when you create a string, every string
that you create Python under the hood
17
what it does is it assigns a number to
each or the items of your string so it
19
starts from 0 so H will be assigned an
index of 0 and then a 1 for I, 2 for the
21
space, 3 for T, five, six
seven, eight.
23
So the exclamation mark character will
will have an index of eight, so you would
25
ask why is this useful? Well this allows
you to extract certain parts from a
27
string and there is a certain notation to
do that you'd want to access the string.
29
So in this case that's variable C
holding the string, then you want to use
31
square brackets so an opening square
bracket and then a closing square
33
bracket and inside those square brackets you
pass the index of the item that you want
35
to extract. Let's say I want the T
character so that would be 0 1 2 3.
37
I pass number 3 there as an index and that
will extract for me the letter T which is
39
actually a string.
So you can check the type of the
41
output and you'll see that that's a
string, so a string is made of strings.
43
You can say so. If you wanted H you'd
past zero and if you wanted a lost
45
character well you might have to count
so one two three four five six seven
47
eight.
And so, and sorry including type there.
49
Sorry about that!
so c8 would give you the exclamation
51
mark, but there's also another indexing
system there that Python uses so in this
53
case we had to count from the beginning
to the end of the string but wouldn't it
55
be useful if you had another system that
lets you count from the end, so from
57
right to left?
Well, such a system exists and it
59
starts from minus 1, so the exclamation
mark would be minus 1 now that you can
61
extract that with that. Minus 2 would be
E, R would be minus 3 and so on up to H, so we
63
have two indexing systems there which
allow you to extract items depending on
65
whether the items are in the beginning
or near the end. Now what if you want to
67
extract more than one item from a string?
Let's say you want to extract Hi.
69
Well there is a notation for that too, so
Hi is expanding from index 0 to index 1.
71
So up to here. So let's try 0 to 1.
That will not work a very good, but let's see.
73
So as I said this will return only H,
so it doesn't return item with index 1.
75
That's because splitting in Python, so we
we're splitting a string here
77
splitting in Python is upper bound
exclusive, which means that the upper
79
bounds of the split here is not included
in the output and so if you want to
81
include I you want to pass the index
after I which is two so this is the string
83
just for reference, so we extracted item
with index 0 and item with index 1.
85
And then the split starts right there at
index 2 so it doesn't include the space.
87
Just hi. If you pass 3 there, that would
include the wide space as well and
89
similarly you can pass like one.
That will include the I and the space, there
91
are also shortcuts, so if you pass zero
like nothing to 3 but that is the same
93
as doing 0 to 3 so it will include 0 1 &
2 and similarly if you want to extract
95
there with the exclamation mark
well what you'd want to do is T would
97
have an index of 0 1 2 3 so 3 and then
you can just pass a column there and
99
execute it and that will give you There
with the exclamation mark. Let's now try
101
to extract R and E using negative
indexing, so something to know is that
103
even though we are using a negative
indexing which starts from right to left
105
the splitting will work from left to
right which means the first index you
107
want to pass is that of the first item.
So R we pass minus 3 for R because
109
you know we have minus 1, minus 2 and minus 3.
So minus 3 for R and then we want E
111
which is minus 2,
but since splitting is upper bounce
113
exclusive we want to pass minus one there.
So let's try that, minus one, execute and yeah
115
we get the correct output.
That's the idea. If you didn't pass
117
anything here you'd get the exclamation
mark as well. So it will take a while
119
until we get used to this, but with some
practice you'll be able to remember
121
these notations. Yeah, that's what I wanted
to cover in this lecture, so this was
123
about string indexing and splitting,
so you split using the index that Python
125
uses on the background. Yeah, I hope
you enjoyed this and I'll talk to you
127
later, thanks!
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.