Lesson 12 - More about strings

advertisement
Day 3 – Lesson 12
More about strings
Python Mini-Course
University of Oklahoma
Department of Psychology
1
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Lesson objectives
1. Use common string methods
2. Use escape sequences to represent
special characters
3. Use string formatting codes to
specify the format of strings for
printing and file output
2
Python Mini-Course: Day 3 – Lesson 12
05/02/09
String methods
 Like everything else in Python,
strings are objects
 Objects have methods that are
invoked using dot notation
object.method([arguments])
3
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Examples
word = 'banana'
new_word = word.upper()
print new_word
index = word.find('a')
print index
4
Python Mini-Course: Day 3 – Lesson 12
05/02/09
String methods
 For a complete listing, see
http://docs.python.org/library/st
dtypes.html#string-methods
5
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Search methods
 str.find(sub[, start[, end]])
print word.find('g',2,4)
 str.index(sub[, start[, end]])
print word.index('g',2,4)
 str.count(sub[, start[, end]])
print word.count('a')
6
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Formatting methods
(return a new string)
 str.lower()
 str.upper()
 str.title()
 str.swapcase()
 str.strip([chars])
 str.lstrip([chars])
 str.rstrip([chars])
7
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Examples:
string_methods2.py
s = ' NOBODY expects the Spanish \
Inquisition!!!'
print s.lower()
print s.upper()
print s.title()
print s.swapcase()
print s.strip()
print s.lstrip()
print s.rstrip('!')
8
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Format checking methods
(return a Boolean value)
 str.isalnum()
 str.isalpha()
 str.isdigit()
 str.islower()
 str.isupper()
 str.istitle()
9
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Splitting and joining*
 str.split([sep[, maxsplit]])
 str.splitlines([keepends])
 str.join(seq)
*We'll revisit these next week
when we cover lists
10
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Special characters
 To express special characters in
Python strings, use Standard C
style escape sequences
 see
http://docs.python.org/reference/lexical_
analysis.html#string-literals
11
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Escape Sequence Meaning
\\
\'
\"
\b
\f
\n
\r
\t
\uxxxx
\xhh
12
Backslash (\)
Single quote (')
Double quote (")
ASCII Backspace (BS)
ASCII Formfeed (FF)
ASCII Linefeed (LF)
ASCII Carriage Return (CR)
ASCII Horizontal Tab (TAB)
Character with 16-bit hex value xxxx
(Unicode only)
Character with hex value hh (7-bit ASCII)
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Example 1:
squares.py
print 'x\ty'
for x in range(1,10):
y = x**2
print str(x) + '\t' + str(y)
13
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Example 1:
squares.py
print 'x\ty\n--\t--'
for x in range(1,10):
y = x**2
print str(x) + '\t' + str(y)
14
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Example 2: square_roots.py
import math
print 'x\ty\n--\t--'
for x in range(1,10):
y = math.sqrt(x)
print str(x) + '\t' + str(y)
15
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Formatting string conversions
 You can use the string formatting
operator % to replace values
within a string
 Example:
s = 'The knights who say %s' % 'Ni'
print s
16
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Formatting string conversions
 You can do multiple replacements
using % within a single string
 Example:
s = 'The %s who say %s' %
('Knights', 'Ni')
print s
17
Python Mini-Course: Day 3 – Lesson 12
\
05/02/09
Improving square_roots.py
import math
print 'x\ty\n--\t--'
for x in range(1,10):
y = math.sqrt(x)
print '%s\t%s' % (x, y)
18
Python Mini-Course: Day 3 – Lesson 12
05/02/09
String formatting codes
 Syntax
%[(name)][flags][width][.precision]code
 Flags
+
space
0
19
left-justify
numeric sign (+/-)
blank before positive numbers
zero fill
Python Mini-Course: Day 3 – Lesson 12
05/02/09
String formatting codes
 Syntax
%[(name)][flags][width][.precision]code
 Width
 Number indicating the total field width
(max number of characters)
 Precision
 Number of digits after the decimal point
20
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Code Meaning
Cod
e
Meaning
Floating-point
exponent
e w/ uppercase
s
String
e
c
d
Character
E
f
i
u
%
21
Decimal
(integer)
Integer
Unsigned
(integer)
Literal '%'
Python Mini-Course: Day 3 – Lesson 12
F
g
G
Floating-point
decimal
f w/ uppercase
e or f
E or F
05/02/09
Examples: integers
x = 59
print 'x
print 'x
print 'x
print 'x
print 'x
print 'x
print 'x
22
=
=
=
=
=
=
=
%d' % x
%+d' % x
%+d%%' % x
%+6d%%' % x
%-6d' % x
%-6d%%' % x
%06d' % x
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Examples: floats
x = 12.3456789
print 'x = %d' % x
print 'x = %f' % x
print 'x = %2.4f' % x
print 'x = %+2.4f' % x
print 'x = %06.2f' % x
23
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Examples: exponential notation
x = 1.34e-6
print 'x = %f' % x
print 'x = %e' % x
print 'x = %g' % x
24
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Example: square_roots2.py
import math
start, stop = 1, 9
print '%s\t%s' % \
('x'.center(3), 'y'.center(6))
print '%s\t%s' % (3*'-', 6*'-')
for x in range(start, stop+1):
y = math.sqrt(x)
print '%3d\t%2.4f' % (x, y)
25
Python Mini-Course: Day 3 – Lesson 12
05/02/09
Download