PYTHON FOR
Strings – Part 1 EVERYBODY
Strings
Chapter 6
Python for Everybody
www.py4e.com
PYTHON FOR
Strings – Part 1 EVERYBODY
>>> str1 = "Hello"
String Data Type >>> str2 = 'there'
>>> bob = str1 + str2
>>> print(bob)
• A string is a sequence of characters Hellothere
>>> str3 = '123'
• A string literal uses quotes >>> str3 = str3 + 1
'Hello' or "Hello" Traceback (most recent call
last): File "<stdin>", line 1,
• For strings, + means “concatenate” in <module>
TypeError: cannot concatenate
• When a string contains numbers, it is 'str' and 'int' objects
still a string >>> x = int(str3) + 1
>>> print(x)
• We can convert numbers in a string into 124
a number using int() >>>
PYTHON FOR
Strings – Part 1 EVERYBODY
Reading and >>> name = input('Enter:')
Enter:Chuck
>>> print(name)
Converting Chuck
>>> apple = input('Enter:')
• We prefer to read data in using
Enter:100
strings and then parse and
>>> x = apple – 10
convert the data as we need
Traceback (most recent call
last): File "<stdin>", line 1,
• This gives us more control over
in <module>
error situations and/or bad user
TypeError: unsupported operand
input
type(s) for -: 'str' and 'int'
>>> x = int(apple) – 10
• Input numbers must be
>>> print(x)
converted from strings
90
PYTHON FOR
Strings – Part 1 EVERYBODY
Looking Inside Strings
• We can get at any single character in a b a n a n a
string using an index specified in 0 1 2 3 4 5
square brackets
>>> fruit = 'banana'
• The index value must be an integer >>>
>>>
letter = fruit[1]
print(letter)
and starts at zero a
>>> x = 3
• The index value can be an expression >>> w = fruit[x - 1]
that is computed >>> print(w)
n
PYTHON FOR
Strings – Part 1 EVERYBODY
A Character Too Far
• You will get a python error >>> zot = 'abc'
>>> print(zot[5])
if you attempt to index Traceback (most recent call
beyond the end of a string. last): File "<stdin>", line
1, in <module>IndexError:
• So be careful when string index out of range
constructing index values >>>
and slices
PYTHON FOR
Strings – Part 1 EVERYBODY
Strings Have Length
b a n a n a
The built-in function len gives us
0 1 2 3 4 5
the length of a string
>>> fruit = 'banana'
>>> print(len(fruit))
6
PYTHON FOR
Strings – Part 1 EVERYBODY
len Function (1 of 2)
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.
'banana' len() 6
(a string) function (a number)
PYTHON FOR
Strings – Part 1 EVERYBODY
len Function (2 of 2)
>>> fruit = 'banana' A function is some stored
>>> x = len(fruit) code that we use. A
>>> print(x) function takes some
6 input and produces an
output.
def len(inp):
blah
'banana' blah 6
for x in y:
(a string) blah
(a number)
blah
PYTHON FOR
Strings – Part 1 EVERYBODY
Looping Through Strings (1 of 3)
Using a while statement 0b
fruit = 'banana'
and an iteration variable, index = 0 1a
and the len 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)
4n
index = index + 1
in a string individually 5a
PYTHON FOR
Strings – Part 1 EVERYBODY
Looping Through Strings (2 of 3)
• A definite loop using a b
for statement is much fruit = 'banana' a
more elegant for letter in fruit: n
print(letter) a
• The iteration variable is n
completely taken care of a
by the for loop
PYTHON FOR
Strings – Part 1 EVERYBODY
Looping Through Strings (3 of 3)
• A definite loop using a fruit = 'banana'
for letter in fruit :
b
for statement is much a
print(letter)
more elegant n
a
• The iteration variable is index = 0 n
completely taken care of while index < len(fruit) : a
by the for loop letter = fruit[index]
print(letter)
index = index + 1
PYTHON FOR
Strings – Part 1 EVERYBODY
Looping and Counting
This is a simple loop that word = 'banana'
loops through each letter in a count = 0
string and counts the number for letter in word :
of times the loop encounters if letter == 'a' :
the 'a' character count = count + 1
print(count)
PYTHON FOR
Strings – Part 1 EVERYBODY
Looking Deeper into in
• The iteration variable “iterates”
through the sequence
(ordered set) Iteration Six-character
variable string
• The block (body) of code is
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
sequence
PYTHON FOR
Strings – Part 1 EVERYBODY
Yes No b a n a n a
Done? Advance letter
print(letter)
for letter in 'banana' :
print(letter)
The iteration variable “iterates” through the string and the block (body)
of code is executed once for each value in the sequence
PYTHON FOR
Strings – Part 1 EVERYBODY
Slicing Strings
M o n t y P y t h o n
(1 of 2)
• We can also look at any 0 1 2 3 4 5 6 7 8 9 10 11
continuous section of a string >>> s = 'Monty Python'
using a colon operator
>>> print(s[0:4])
• The second number is one Mont
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 Python
beyond the end of the string,
it stops at the end
PYTHON FOR
Strings – Part 1 EVERYBODY
Slicing Strings
M o n t y P y t h o n
(2 of 2) 0 1 2 3 4 5 6 7 8 9 10 11
If we leave off the first number or >>> 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:])
thon
>>> print(s[:])
Monty Python
PYTHON FOR
Strings – Part 1 EVERYBODY
Manipulating Strings..
PYTHON FOR
Strings – Part 1 EVERYBODY
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
(www.dr-chuck.com) of the University of Michigan School of
Information and open.umich.edu and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change,
feel free to add your name and organization to the list of
contributors on this page as you republish the materials.
Initial Development: Charles Severance, University of Michigan
School of Information
… Insert new Contributors and Translators here