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

'this is a thistle with many thorns'.count('th')
'haystack containing needle and straw'.find('needle')
  • find() returns position - -1 if not found

'haystack containing needle and straw'.find('mouse')
  • index() returns position - raises if not found

'haystack containing needle and straw'.index('mouse')
ValueError                                Traceback (most recent call last)
Cell In[6], line 1
----> 1 'haystack containing needle and straw'.index('mouse')

ValueError: substring not found

Split and Join (1)

  • Very common operations

  • Error prone ⟶ writing them is a major annoyance

  • Off-by-one errors

['one', 'two', 'three']
':'.join(['one', 'two', 'three'])

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)