python mini-course university of oklahoma department of psychology day 3 – lesson 12 more about...

25
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12 1

Upload: camron-ellis

Post on 13-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Python Mini-CourseUniversity of Oklahoma

Department of Psychology

Day 3 – Lesson 12More about strings

05/02/09Python Mini-Course: Day 3 – Lesson 121

Page 2: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Lesson objectives

1. Use common string methods2. Use escape sequences to represent

special characters3. Use string formatting codes to

specify the format of strings for printing and file output

05/02/09Python Mini-Course: Day 3 – Lesson 122

Page 3: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

String methods

Like everything else in Python, strings are objectsObjects have methods that are invoked using dot notationobject.method([arguments])

05/02/09Python Mini-Course: Day 3 – Lesson 123

Page 4: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Examples

word = 'banana'new_word = word.upper()print new_word

index = word.find('a')print index

05/02/09Python Mini-Course: Day 3 – Lesson 124

Page 5: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

String methods

For a complete listing, seehttp://docs.python.org/library/stdtypes.html#string-methods

05/02/09Python Mini-Course: Day 3 – Lesson 125

Page 6: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

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')

05/02/09Python Mini-Course: Day 3 – Lesson 126

Page 7: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Formatting methods(return a new string)

str.lower()str.upper()str.title()str.swapcase()str.strip([chars])str.lstrip([chars])str.rstrip([chars])

05/02/09Python Mini-Course: Day 3 – Lesson 127

Page 8: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

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('!')

05/02/09Python Mini-Course: Day 3 – Lesson 128

Page 9: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Format checking methods(return a Boolean value)

str.isalnum()str.isalpha()str.isdigit()str.islower()str.isupper()str.istitle()

05/02/09Python Mini-Course: Day 3 – Lesson 129

Page 10: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Splitting and joining*

str.split([sep[, maxsplit]])str.splitlines([keepends])str.join(seq)

*We'll revisit these next week when we cover lists

05/02/09Python Mini-Course: Day 3 – Lesson 1210

Page 11: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Special characters

To express special characters in Python strings, use Standard C style escape sequencessee

http://docs.python.org/reference/lexical_analysis.html#string-literals

05/02/09Python Mini-Course: Day 3 – Lesson 1211

Page 12: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

05/02/09Python Mini-Course: Day 3 – Lesson 1212

Escape Sequence

Meaning

\\ Backslash (\)

\' Single quote (')

\" Double quote (")

\b ASCII Backspace (BS)

\f ASCII Formfeed (FF)

\n ASCII Linefeed (LF)

\r ASCII Carriage Return (CR)

\t ASCII Horizontal Tab (TAB)

\uxxxx Character with 16-bit hex value xxxx (Unicode only)

\xhh Character with hex value hh (7-bit ASCII)

Page 13: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Example 1: squares.py

print 'x\ty'for x in range(1,10): y = x**2 print str(x) + '\t' + str(y)

05/02/09Python Mini-Course: Day 3 – Lesson 1213

Page 14: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Example 1: squares.py

print 'x\ty\n--\t--'for x in range(1,10): y = x**2 print str(x) + '\t' + str(y)

05/02/09Python Mini-Course: Day 3 – Lesson 1214

Page 15: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Example 2: square_roots.py

import mathprint 'x\ty\n--\t--'for x in range(1,10): y = math.sqrt(x) print str(x) + '\t' + str(y)

05/02/09Python Mini-Course: Day 3 – Lesson 1215

Page 16: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

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

05/02/09Python Mini-Course: Day 3 – Lesson 1216

Page 17: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Formatting string conversions

You can do multiple replacements using % within a single string

Example: s = 'The %s who say %s' % \ ('Knights', 'Ni') print s

05/02/09Python Mini-Course: Day 3 – Lesson 1217

Page 18: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Improving square_roots.py

import mathprint 'x\ty\n--\t--'for x in range(1,10): y = math.sqrt(x) print '%s\t%s' % (x, y)

05/02/09Python Mini-Course: Day 3 – Lesson 1218

Page 19: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

String formatting codes

Syntax %[(name)][flags][width][.precision]code

Flags - left-justify

+ numeric sign (+/-)

space blank before positive numbers

0 zero fill

05/02/09Python Mini-Course: Day 3 – Lesson 1219

Page 20: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

String formatting codes

Syntax %[(name)][flags][width][.precision]code

WidthNumber indicating the total field width

(max number of characters)Precision

Number of digits after the decimal point

05/02/09Python Mini-Course: Day 3 – Lesson 1220

Page 21: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

05/02/09Python Mini-Course: Day 3 – Lesson 1221

Code Meaning Code

Meaning

s String e Floating-point exponent

c Character E e w/ uppercase

d Decimal (integer)

f Floating-point decimal

i Integer F f w/ uppercase

u Unsigned (integer)

g e or f

% Literal '%' G E or F

Page 22: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Examples: integers

x = 59print 'x = %d' % xprint 'x = %+d' % xprint 'x = %+d%%' % xprint 'x = %+6d%%' % xprint 'x = %-6d' % xprint 'x = %-6d%%' % xprint 'x = %06d' % x

05/02/09Python Mini-Course: Day 3 – Lesson 1222

Page 23: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Examples: floats

x = 12.3456789print 'x = %d' % xprint 'x = %f' % xprint 'x = %2.4f' % xprint 'x = %+2.4f' % xprint 'x = %06.2f' % x

05/02/09Python Mini-Course: Day 3 – Lesson 1223

Page 24: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Examples: exponential notation

x = 1.34e-6print 'x = %f' % xprint 'x = %e' % xprint 'x = %g' % x

05/02/09Python Mini-Course: Day 3 – Lesson 1224

Page 25: Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12

Example: square_roots2.py

import mathstart, stop = 1, 9print '%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)

05/02/09Python Mini-Course: Day 3 – Lesson 1225