PT BR Py4Inf 06 Strings To Be Translated
PT BR Py4Inf 06 Strings To Be Translated
Chapter 6
b a n a n a
0 1 2 3 4 5
• There is a built-in function len that
gives us the length of a string >>> fruit = 'banana'
>>> print len(fruit)
6
Len Function
>>> 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 number)
(a string) function
print letter
The iteration variable “iterates” though the string and the block
(body) of code is executed once for each value in the sequence
M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
• We can also look at any
>>> s = 'Monty
continuous section of a string
using a colon operator Python'
>>> 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
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
>>> a = 'Hello'
• When the + operator is >>> b = a + 'There'
>>> print b
applied to strings, it
means "concatenation" HelloThere
>>> c = a + ' ' + 'There'
>>> print c
Hello There
>>>
Using in as an Operator
• The in keyword can also be
>>> fruit = 'banana’
>>> 'n' in fruit
used to check to see if one True
string is "in" another string >>> 'm' in fruit
False
• The in expression is a >>> 'nan' in fruit
True
logical expression and
>>> if 'a' in fruit :
returns True or False and ... print 'Found it!’
can be used in an if ...
statement Found it!
>>>
String Comparison
if word == 'banana':
print 'All right, bananas.'
http://docs.python.org/lib/string-methods.html
http://docs.python.org/lib/string-methods.html
String Library
http://docs.python.org/lib/string-methods.html
Searching a String
• We use the find() function to search
b a n a n a
for a substring within another string 0 1 2 3 4 5
• find() finds the first occurrence of >>> fruit = 'banana'
the substring >>> pos =
fruit.find('na')
• If the substring is not found, find() >>> print pos
returns -1 2
>>> aa = fruit.find('z')
• Remember that string position >>> print aa
-1
starts at zero
Making everything UPPER CASE
• You can make a copy of a string in >>> greet = 'Hello Bob'
lower case or upper case >>> nnn = greet.upper()
• Often when we are searching for
>>> print
HELLO BOB
nnn
a string using find() - we first
>>> www = greet.lower()
convert the string to lower case
>>> print www
so we can search a string
hello bob
regardless of case
>>>
Search and Replace
• The replace()
function is like a
>>> greet = 'Hello Bob'
“search and replace” >>> nstr =
operation in a word greet.replace('Bob','Jane')
processor >>> print nstr
Hello Jane
• It replaces all >>> nstr = greet.replace('o','X')
>>> print nstrHellX BXb
occurrences of the
>>>
search string with the
replacement string
Stripping Whitespace
• Sometimes we want to take a
string and remove whitespace at >>> greet = ' Hello Bob '
the beginning and/or end >>> greet.lstrip()
'Hello Bob '