Python startswith - How to check if a string starts with another string

The startswith() checks if a Python string starts with a specific substring.

a = 'Microsoft'

if a.startswith('M'):
    print('True')
else:
    print('False')

# True

Microsoft starts with M so the method returns true. We need to make that kind of function in some programming languages but don't need it in Python. The startswith() distinguishes the uppercase letters and lowercase letters.

a = 'Microsoft'

if a.startswith('m'):
    print('True')
else:
    print('False')

# False

All strings start with an empty string

All strings start with '' as follows.

a = 'Microsoft'

if a.startswith(''):
    print('True')
else:
    print('False')

# True

Microsoft starts with an empty string. It's a tiny example but interesting.

Python String

Python Tutorial