Miscellaneous String Methods

Batteries Included: Checks

Lots of small checks (returning boolean) - for example …

  • '...'.isspace(): contains only whitespace

  • Character types

    • '...'.isalpha()

    • '...'.isalnum()

    • '...'.isdigit()

  • Case tests

    • '...'.isupper()

    • '...'.islower()

  • '...'.isidentifier(): a valid python identifier (e.g. variable name)

  • Lots of others ⟶ save work and RTFM prior to coding

Substring Search: Examples

>>> '/etc/passwd'.startswith('/etc/')
True
>>> 'notes.txt'.endswith('.txt')
True
>>> 'this is a thistle with many thorns'.count('th')
4
>>> 'haystack containing needle and straw'.find('needle')
20
>>> 'haystack containing needle and straw'.find('mouse')
-1
>>> 'haystack containing needle and straw'.index('mouse')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

Split and Join (1)

  • Very common operations

  • Error prone ⟶ writing them is a major annoyance

  • Off-by-one errors

split() and join()
>>> 'one:two:three'.split(':')
['one', 'two', 'three']
>>> ':'.join(['one', 'two', 'three'])
'one:two:three'
Not off-by-one
>>> ':'.join([])
''
>>> ':'.join(['one'])
'one'

Split and Join (2)

Split at most 2 fields
>>> 'one:two:three:four'.split(':', 2)
['one', 'two', 'three:four']
>>> 'one:two:three:four'.rsplit(':', 2)
['one:two', 'three', 'four']
Real life example: /etc/passwd
>>> username,rest = 'jfasch:x:1000:...'.split(':', 1)
>>> username
'jfasch'
>>> rest
'x:1000:1000::/home/jfasch:/bin/bash'