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

(andrew) #1

to stop at box 3 (but don’t want to open the box). If you
want to print the whole string, just pick a number
beyond the index value, and Python will print everything,
as in this example:


>>> a[0:6]
'DevNet'

If you omit a value for the first number, Python starts at
0, as in this example:


>>> a[:2]
'De'

If you omit the second value, Python prints to the end of
the string, as in this example:


>>> a[2:]
'vNet'

You can also reverse direction by using negative
numbers. If you put a negative first number, you start
from the end of the string, as in this example:


>>> a[-2:]
'et'

A negative value on the other side of the colon causes
Python to print using the end as a reference point, as in
this example:


>>> a[:-2]
'DevN'

You can perform math operations on strings as well. The



  • is used to add or concatenate two strings together, as

Free download pdf