Dies ist die Support Website des Buches:

Das Python Praxisbuch
Der große Profi-Leitfaden für Programmierer
Farid Hajji
Addison Wesley / Pearson Education
ISBN 978-3-8273-2543-3 (Sep 2008), 1298 Seiten.

5. Strings

Note

Bei Python 3.X sind alle Strings standardmäßig unicode-Strings, und die Klasse basestring gibt es nicht mehr. Hinweise zu Python 3.X Unterschiede fehlen noch.

Einfache Bytestrings

String-Literale

#!/usr/bin/env python
# literalstrings.py -- writing literal strings.

# A singly-quoted string.
s1 = '<a href="http://www.example.com/">link</a>'

# A doubly-quoted string
s2 = "Joe's Appartment"

# A triply-quoted string with double quotes.
s3 = """foo(string) -> string

Transmogrify string, doing this and that.
Return the transmogrified string."""

# A triply-quoted string with single quotes.
s4 = '''<html>
  <head>
     <title>A title</title>
  </head>
  <body>
    <h1>A test page</h1>
    <div>
      <a href="http://www.example.com/">Back home</a>.
    </div>
  </body>
</html>'''

literalstrings.py

String Interpolation

String Slices

String-Operatoren

String-Methoden

Screenshots:

Unicode-Strings

Unicode-Encodings

Der unicode-Datentyp

Codieren und Decodieren von Unicode

URLs:

Codecs

Reguläre Ausdrücke

Was sind reguläre Ausdrücke?

URLs:

re.search und re.sub

Kompilierte reguläre Ausdrücke

Das Match-Objekt

Die Flags

findall und finditer

Anwendungen

Suchen in Strings

#!/usr/bin/env python
# stringsearch.py -- searching in strings with string methods

s   = raw_input('Enter source string: ')
sub = raw_input('Enter substring: ')

# The in operator returns True or False:
if sub in s:
    print "'%s' is a substring of '%s'" % (sub, s)
else:
    print "'%s' is NOT a substring of '%s'" % (sub, s)

# index, rindex return index (0-based), or raise ValueError:
try:
    idx = s.index(sub)
    ridx = s.rindex(sub)
    print "'%s'.index('%s') == %d" % (s, sub, idx)
    print "'%s'.rindex('%s') == %d" % (s, sub, ridx)
except ValueError:
    print "'%s' doesn't occur in '%s'" % (sub, s)

# find, rfind return index (0-based), or -1 if not found
pos = s.find(sub)
rpos = s.rfind(sub)
print "'%s'.find('%s') == %d" % (s, sub, pos)
print "'%s'.rfind('%s') == %d" % (s, sub, rpos)

# startswith, endswith return True or False
print "'%s'.startswith('%s') == " % (s, sub), s.startswith(sub)
print "'%s'.endswith('%s') == " % (s, sub), s.endswith(sub)

# count returns number of non-overlapping occurences:
print "'%s' occurs %d times in '%s'" % (sub, s.count(sub), s)

stringsearch.py

Strings effizient aufbauen

#!/usr/bin/env python
# stringbuild.py -- shows how to build a string efficiently

def ul_inefficient(list_of_items):
    "Create and return a <ul> list of <li> items as string."
    s = '<ul>'
    for item in list_of_items:
        s = s + '\n<li>%s</li>' % escape_html(item)
    s = s + '\n</ul>'
    return s

def ul_efficient(list_of_items):
    "Create and return a <ul> list of <li> items as string."
    slist = ['<ul>']
    for item in list_of_items:
        slist.append('<li>%s</li>' % escape_html(item))
    slist.append('</ul>')
    return '\n'.join(slist)

def escape_html(s):
    '''Escape HTML special characters.

    Given a string s, escape the HTML special characters
    "&", "<" and ">". Return the escaped string.'''

    return s.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')

if __name__ == '__main__':
    thelist = [ 'Python', 'Ruby', 'Perl', 'PHP' ]
    ul_string1 = ul_inefficient(thelist)
    ul_string2 = ul_efficient(thelist)
    assert ul_string1 == ul_string2

stringbuild.py

Zusammenfassung