DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

always start with 0, so if you want the very first character
in a string, your index value would be 0 and not 1. Figure
3-3 shows the string DevNet with its corresponding
index values.


Figure 3-3 DevNet String Index


If you assign the string DevNet to a variable, you can
separate and manipulate the component values of the
string by using the index value. You can use brackets to
specify the index number. The following example prints a
capitol D from DevNet:


>>> a='DevNet'
>>> a[0]
'D'

You can also specify ranges to print. The colon operator
gives you control over whole sections of a string. The first
number is the beginning of the slice, and the second
number determines the end. The second number may be
confusing at first because it is intended to identify “up to
but not including” the last character. Consider this
example:


>>> a[0:3]
'Dev'

This example shows a 3 at the end, but this is technically
four characters since the index starts at 0, but Python
doesn’t print the last character and instead stops right
before it. For new Python programmers, this can be
confusing, but remember that Python is literal. If you
think of an index value as a box, in Figure 3-3, you want

Free download pdf