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

(andrew) #1
!= Not equal to 5 != “5” True

Strings


The string data type is a sequence of characters and uses
quotes to determine which characters are included. The
string ‘Hello’ is just a set of characters that Python
stores in order from left to right. Even if a string contains
a series of numbers, it can still be a string data type. If
you try to add a 1 to a string value, Python gives you an
error, as shown in this example:


Click here to view code image


>>> '10' + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to
str

This error tells you that you have to convert the string to
an integer or another data type to be able to use it as a
number (in a math formula, for example). The int()
function can convert a string value into an integer for
you, as shown in this example:


>>> int('10') + 1
11

A string is just a list of characters in a certain order that
Python keeps track of. In fact, this aspect of strings
makes them easy to manipulate. If you use the string
'DevNet', you can pull out any individual characters of
the string by knowing where it sits in the string index.
One thing to keep in mind is that indexes in Python

Free download pdf