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
- Check if a string contains the other string
- Check if a string starts with another string
- Split a string
- Concatenate strings
- Get the index of substring
- Get the index of the last occurrence of a substring
- Trim a string
- Replace a substring
- Replace multiple spaces with one space
- Fill 0 letters to a string
- Capitalize a string
- Convert a string lowercase
- Check if a string is numeric
- Check if a string is uppercase
- Parse a string to a float
- Format a string
- f-string
- Reverse a string
- Slice a string
- Subtract a string from another string
- Make a random string
- Generate a secure token
- Encode a string
- Get the sha256 value
- Convert an integer to a hexadecimal string
- Get a character or integer value
- Get digits or ASCII letters
- Center a string
- Convert string to tuple