Python-Basics
Strings-Text Processing
Guru
Page 1
Agenda
• Escape Sequences
• Raw Strings
• Doc Strings
• More formatting
• Conversions
• String Methods
• The string module
Page 2
Escapes and Special Characters
• Certain special characters have special values
• They do not have a corresponding print character
• Instead they have an effect
• For example, newline, carriage return
• To insert a special character, you “escape” it
• Using the backslash
Page 3
Escape Characters
• Newline ('n')
• Carriage return ('r')
• Single quote (''')
• Double quote (""")
• Backspace ('b')
• Tab ('t')
• Vertical tab ('v')
• Backslash ('')
Page 4
Raw Strings
• The raw_input( ) function reads a line from
standard input as a raw string
• Used in interactive console programs
Page 5
Quoting
• Can use either single or double
• This makes embedded quotes easy
• Can also “escape” characters with 
• Triple quotes define “long strings”
• String that can span multiple lines
• Often used in documentation and web pages
Page 6
str( ) vs. repr( )
• str( ) converts an object to a string
• It renders a human-readable string
• print uses str( ) (and expands escapes)
• repr( ) renders a string that can be evaluated by the
interpreter
• As if typed at the >>> prompt
• So it executes correctly
• Also can use backquotes
• The interpreter uses repr( ) to echo variables
• Differences show up in numbers and escape
sequences
Page 7
str( ) vs. repr( )
Examples
>>> x = .1
>>> x
0.10000000000000001
>>> str(x)
'0.1‘
>>> repr(x)
'0.10000000000000001‘
>>> `x`
'0.10000000000000001‘
>>> s = "Hellonthere"
>>> s
'Hellonthere'
>>> print s
Hello
there
>>> repr(s)
"'Hellonthere'"
Page 8
Question
• What is the result of executing:
str(2+3)
?
Page 9
Format Descriptors
• %s (string, via str( ))
• %r (string, via repr( ))
• %c (character)
• %d (decimal integer)
• %x (hex integer)
• %X (uppercase hex)
• %e (scientific notation for reals)
• %f (fixed-point decimal for reals)
• %g (attempts %f; bails to %e)
Page 10
String Methods
• A lot!
• Called as <string-var>.<method>(…)
• For example, s.count('a')
• capitalize, center, count, endswith, find,
index, isalpha, isdigit (etc., as in C), istitle,
join, lower, replace, split, strip, swapcase,
title, translate, upper, zfill
Page 11
Using split( )
• Splits a string into substrings
• Uses whitespace as a separator(default)
• You can provide your own separator
• It uses the whole separator string
• See next slide
• The re module has a better version of split
Page 12
Using split( ) and join( )
Example
>>> s = 'a,b,c d,e‘
>>> s.split()
['a,b,c', 'd,e']
>>> s.split(',')
['a', 'b', 'c d', 'e']
>>> ','.join(['x','y','z'])
'x,y,z‘
>>> ''.join(['x','y','z'])
'xyz'
Page 13
Exercise
• Print the words from a sentence in the
reverse order of the appearance,
• Write a module that reads a string from the
console and tests to see if it is a palindrome
(spelled the same backward as forward)
(Hint: for both of these, the reverse( ) list method comes in
handy)
Page 14
Questions
Page 15
Imagination Action Joy

Strings.ppt

  • 1.
  • 2.
    Page 1 Agenda • EscapeSequences • Raw Strings • Doc Strings • More formatting • Conversions • String Methods • The string module
  • 3.
    Page 2 Escapes andSpecial Characters • Certain special characters have special values • They do not have a corresponding print character • Instead they have an effect • For example, newline, carriage return • To insert a special character, you “escape” it • Using the backslash
  • 4.
    Page 3 Escape Characters •Newline ('n') • Carriage return ('r') • Single quote (''') • Double quote (""") • Backspace ('b') • Tab ('t') • Vertical tab ('v') • Backslash ('')
  • 5.
    Page 4 Raw Strings •The raw_input( ) function reads a line from standard input as a raw string • Used in interactive console programs
  • 6.
    Page 5 Quoting • Canuse either single or double • This makes embedded quotes easy • Can also “escape” characters with • Triple quotes define “long strings” • String that can span multiple lines • Often used in documentation and web pages
  • 7.
    Page 6 str( )vs. repr( ) • str( ) converts an object to a string • It renders a human-readable string • print uses str( ) (and expands escapes) • repr( ) renders a string that can be evaluated by the interpreter • As if typed at the >>> prompt • So it executes correctly • Also can use backquotes • The interpreter uses repr( ) to echo variables • Differences show up in numbers and escape sequences
  • 8.
    Page 7 str( )vs. repr( ) Examples >>> x = .1 >>> x 0.10000000000000001 >>> str(x) '0.1‘ >>> repr(x) '0.10000000000000001‘ >>> `x` '0.10000000000000001‘ >>> s = "Hellonthere" >>> s 'Hellonthere' >>> print s Hello there >>> repr(s) "'Hellonthere'"
  • 9.
    Page 8 Question • Whatis the result of executing: str(2+3) ?
  • 10.
    Page 9 Format Descriptors •%s (string, via str( )) • %r (string, via repr( )) • %c (character) • %d (decimal integer) • %x (hex integer) • %X (uppercase hex) • %e (scientific notation for reals) • %f (fixed-point decimal for reals) • %g (attempts %f; bails to %e)
  • 11.
    Page 10 String Methods •A lot! • Called as <string-var>.<method>(…) • For example, s.count('a') • capitalize, center, count, endswith, find, index, isalpha, isdigit (etc., as in C), istitle, join, lower, replace, split, strip, swapcase, title, translate, upper, zfill
  • 12.
    Page 11 Using split() • Splits a string into substrings • Uses whitespace as a separator(default) • You can provide your own separator • It uses the whole separator string • See next slide • The re module has a better version of split
  • 13.
    Page 12 Using split() and join( ) Example >>> s = 'a,b,c d,e‘ >>> s.split() ['a,b,c', 'd,e'] >>> s.split(',') ['a', 'b', 'c d', 'e'] >>> ','.join(['x','y','z']) 'x,y,z‘ >>> ''.join(['x','y','z']) 'xyz'
  • 14.
    Page 13 Exercise • Printthe words from a sentence in the reverse order of the appearance, • Write a module that reads a string from the console and tests to see if it is a palindrome (spelled the same backward as forward) (Hint: for both of these, the reverse( ) list method comes in handy)
  • 15.
  • 16.