Py4Inf 06 Strings
Py4Inf 06 Strings
Chapter 6
Unless otherwise noted, the content of this course material is licensed under a Creative
Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/.
Copyright 2010- Charles Severance
String Data
Type
A string is a sequence of
characters
Reading and
Converting
We prefer to read data in
using strings and then
parse and convert the
data as we need
>>> name =
raw_input('Enter:')
Enter:Chuck
>>> print name
Chuck
>>> apple =
raw_input('Enter:')
Enter:100
>>> x = apple 10
Traceback (most recent call
last): File "<stdin>", line 1, in
<module>TypeError:
unsupported operand type(s)
for -: 'str' and 'int'
>>> x = int(apple) 10
b a n a n a
0 1 2 3 4 5
>>>
>>>
>>>
a
>>>
>>>
>>>
n
fruit = 'banana'
letter = fruit[1]
print letter
n=3
w = fruit[n - 1]
print w
So be careful when
b a n a n a
0 1 2 3 4 5
>>> fruit = 'banana'
>>> print len(fruit)
6
Len Function
A function is some
stored code that we
use. A function takes
some input and
produces an output.
'banana'
(a string)
len()
function
Guido wrote this code
6
(a number)
Len Function
A function is some
stored code that we
use. A function takes
some input and
produces an output.
'banana'
(a string)
def len(inp):
blah
blah
for x in y:
blah
blah
6
(a number)
fruit = 'banana'
index = 0
while index < len(fruit)
letter = fruit[index]
print index, letter
index = index + 1
0
1
:2
3
4
5
b
a
n
a
n
a
fruit = 'banana'
for letter in fruit :
print letter
b
a
n
a
n
a
fruit = 'banana'
for letter in fruit :
print letter
b
a
n
a
index = 0
n
while index < len(fruit) :
a
letter = fruit[index]
print letter
index = index + 1
word = 'banana'
count = 0
for letter in word :
if letter == 'a' :
count = count + 1
print count
Iteration variable
Six-character string
Yes
Done?
Advance
letter
b a n a n a
print letter
M o n t y
P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
Slicing
Strings
M o n t y
P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
Slicing
Strings
String Concatenation
When the + operator
is applied to strings,
it means
"concatenation"
>>> a = 'Hello'
>>> b = a + 'There'
>>> print b
HelloThere
>>> c = a + ' ' + 'There'
>>> print c
Hello There
>>>
Using in as an Operator
The in expression is a
String Comparison
if word == 'banana':
print 'All right, bananas.'
if word < 'banana':
print 'Your word,' + word + ', comes before banana.
elif word > 'banana':
print 'Your word,' + word + ', comes after banana.
else:
print 'All right, bananas.'
String Library
Python has a number of string
http://docs.python.org/lib/string-methods.html
http://docs.python.org/lib/string-methods.html
String Library
str.capitalize()
str.replace(old, new[, count])
str.center(width[, fillchar])
str.lower()
str.endswith(suffix[, start[, end]]) str.rstrip([chars])
str.find(sub[, start[, end]])
str.strip([chars])
str.lstrip([chars])
str.upper()
http://docs.python.org/lib/string-methods.html
Searching a
String
b a n a n a
0 1 2 3 4 5
>>>
>>>
>>>
2
>>>
>>>
-1
fruit = 'banana'
pos = fruit.find('na')
print pos
aa = fruit.find('z')
print aa
function is like a
search and
replace operation
in a word processor
It replaces all
occurrences of the
search string with
the replacement
string
Stripping Whitespace
Sometimes we want to
Prefixes
>>> line = 'Please have a nice day
>>> line.startswith('Please')
True
>>> line.startswith('p')
False
21
31
Summary
String type
Read/Convert
Indexing strings []
Slicing strings [2:4]
Looping through strings with for and while
Concatenating strings with +
String operations