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
  • find() returns position - -1 if not found

'haystack containing needle and straw'.find('mouse')
-1
  • 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'.split(':')
['one', 'two', 'three']
':'.join(['one', 'two', 'three'])
'one:two:three'
':'.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:...'