[Fixed]-Python VBA-like left()

1👍

This checks if the first letter in mystring is 'a' or 'b'.

if mystring[0] in ('a', 'b'):
    # whatever

Use slicing to test for longer substrings:

if mystring[:3] in ('abc', 'def', '987'):
    # whatever

Alternatively you can use str.startswith():

if mystring.startswith(('SUBTOTAL', 'TOTAL')):
    # whatever

Leave a comment