Pees
See ron
Strings
Chapter 6
Python for EverybodySee
String Data Type
Soret eae Seer
+ Astring literal uses quotes
Bal ool a (clo
Sune Rescued cic
+ When a string contains numbers, it is
still a string
+ We can convert numbers in a string into
a number using int()
Pees
ron
>>> strl = "Hello"
>>> str2 = ‘there’
>>> bob Pits ee 7d
poomere ta tstas))
Hellothere
coos eee
>>> str3 = str3 +1
bee aa oa
last): File "", line 1,
Pee y-Ubt oS
TypeError: cannot concatenate
‘str' and 'int' objects
>>> x = int(str3) +1
>>> print (x)
eZ)
oodSee
Reading and
Converting
Serene mt)
Suen ueun ec
Crue Cee net
Seon Re cone
error situations and/or bad user
ang
Samael eae 3
Pen COMES Uae
Pees
ron
>>> name = input('Enter:')
Enter: Chuck
>>> print (name)
Chuck
>>> apple = input ('Enter:')
Enter: 100
>>> x = apple - 10
be So Cae to tac
cet eee eRe eae
Beep Ube
TypeError: unsupported operand
type(s) for -: 'str' and ‘int!
eee SEE LAC) eT
>>> print (x)
90See
Pees
ron
Looking Inside Strings
° We can get at any single character in a
string using an index specified in
square brackets
bane DAU Miele cles
and starts at zero
° The index value can be an expression
Har\mcmero lial eelccre|
012345
>>> fruit = 'banana'
>>> letter = fruit[1]
Poon Seta Okt)
EN
Domes
po es rae ae
>>> print (w)
cyPees
See ron
A Character Too Far
* You will get a python error BEE OES aes!
if you attempt to index id Leesa
A Traceback (most recent call
beyond the end of a string. Tee bie teee eer
0 1, in IndexError:
So be careful when Pirate Meee Cs MO ar Ter
constructing index values booed
Fale lo}Pees
See ron
Strings Have Length
DAMAGE
MiaT-m ell icolamiP lac Coleman: oom 0123465
the length of a string
poo me sath AEB tL
>>> print (len (fruit) )
(Pees
See ron
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print (x) function takes some
(J input and produces an
output.
oF Tat=alc i)
(a string) (a number)Pero gae.g
See roo
len Function
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) ferelelR UTA RUS oaraN
>>> print (x) function takes some
(J input and produces an
output.
banana iS
(a string) (a number)Pees
See ron
Looping Through Strings
Using a while statement - Ob
pra ook ee Cc e
and an iteration Na ee ae oi
and the \en function, we while index < len(fruit): 2N
can construct a loop to letter = fruit[index] 3a
look at each of the letters print (index, letter) ra
in a string individually AGGIES teats pt ryPees
See ron
Looping Through Strings
baer We -viiai Moco MUCH aTe I}
eS lua Sao fruit = ‘banana’
er for letter in fruit:
bam -M e110) (oa peEOA)
felon nyel =i CMC Lleol
by the for loop
tie odPees
SU ee ry
be Me(-iila i CM LeceloRU Sine gE} Foot CCT b
for statement is much Gee Howse dh Gates @ a
(iste) Melereretan Peet osa sy) i
Ey
aM Mile LE1e) My fey n
completely taken care of = while index < len(fruit) E)
by the for loop letter = fruit[index]
print (letter)
index = index + 1See
Pees
ron
Looping and Counting
This is a simple loop that
loops through each letter in a
string and counts the number
of times the loop encounters
Lule ete cls
word = 'banana'
count = 0
for letter in word :
ES "at
count = count + 1
print (count)Pees
See rong
Looking Deeper into in
» The iteration variable “iterates”
Sale 0le lM nt -MetetelN elas
One ien) orton Ses
NE TaEV91(-y string
* The block (body) of code is IN
executed once for each value
in the sequence for letter in 'banana'
print (letter)
* The iteration variable moves
through all of the values in the
sequencePees
See ron
aA
Pela ccs
print(letter)
for letter in 'banana'
poset OR Sato]
The iteration variable “iterates” through the string and the block (body)
of code is executed once for each value in the sequencePees
ron
Slicing Strings [mJo[n| ANE
012346567 8 9 1011
See
+ We can also look at any
continuous section of a string
‘ >>> s = 'Monty Python'
using a colon operator
>>> print(s[0:4])
+ The second number is one pag
beyond the end of the slice - >>> print(s[6:7])
“up to but not including” P
>>> print (s[6:20])
+ If the second number is Bs Siaatossy
beyond the end of the string,
it stops at the endPees
See ron
Slicing Strings [oT] ANE
2345 67 8 9 1011
AVC Mea RT eae mel >>> s = ‘Monty Python'
the last number of the slice, it is >>> print(s[:2])
assumed to be the beginning or Mo
end of the string respectively >>> print (s[8:])
alstoSey
>>> print(s[:])
Monty Pythona Cc
MELT Ue Kaiya alt oeSUL eee
Acknowledgements / Contributions
“These slides are Copyright 2010- Charles R. Severance
r ene eee
ee eae phere)
eee ne ene eee aT
last slide in all copies of the document to comply with the
ee ce
ee ene tae
ee ans
eee een ee
Pees
Insert new Contributors and Translators here
Pero gae.g
ron