- Syntax
- Example 1: Checking the Beginning of a String
- Example 2: Checking the End of a String
- Example 3: Using isalpha()
- Example 4: Using isdigit()
- Example 5: Using isalnum()
- Example 6: User Input Validation
- Example 7: Alphabetic Check with Spaces
- Example 8: Checking Numeric Input
- Example 9: Case-Sensitive Prefix Check
- Example 10: Checking the Result Type
- Common Mistakes
- Best Practices
- Key Points to Remember
Python provides many string methods for checking the contents of a string. These methods return Boolean values (True or False) based on whether a particular condition is satisfied.
These methods are commonly used for validating user input, checking passwords, verifying data, and making decisions in programs.
In this lesson, we will study the following commonly used string methods:
startswith()endswith()isalpha()isdigit()isalnum()
Syntax #
string.method(argument)
or
string.method()
Components #
| Component | Description |
|---|---|
string |
The string object. |
. |
Dot operator used to access the method. |
method() |
String method being called. |
argument |
Optional value required by some methods. |
Example 1: Checking the Beginning of a String #
main.py
text = "Python Programming"
print(text.startswith("Python"))
Output #
True
Explanation #
- The
startswith()method checks whether the string begins with"Python". - Since the condition is satisfied, it returns
True. - The comparison is case-sensitive.
Example 2: Checking the End of a String #
main.py
filename = "report.pdf"
print(filename.endswith(".pdf"))
Output #
True
Explanation #
endswith()checks whether the string ends with.pdf.- Since the filename has the required extension, the result is
True. - This method is commonly used when validating file names.
Example 3: Using isalpha() #
main.py
word = "Python"
print(word.isalpha())
Output #
True
Explanation #
isalpha()checks whether all characters are alphabetic.- The string contains only letters.
- Therefore, the result is
True.
Example 4: Using isdigit() #
main.py
number = "12345"
print(number.isdigit())
Output #
True
Explanation #
isdigit()checks whether every character is a digit.- Since all characters are numeric, the method returns
True.
Example 5: Using isalnum() #
main.py
text = "Python123"
print(text.isalnum())
Output #
True
Explanation #
isalnum()checks whether all characters are either letters or digits.- The string contains only alphabets and numbers.
- Therefore, the result is
True.
Example 6: User Input Validation #
main.py
username = input("Enter username: ")
print(username.isalnum())
Sample Input #
Alice123
Output #
True
Explanation #
- The user enters a username.
isalnum()checks whether it contains only letters and digits.- Since no special characters are present, the result is
True.
Example 7: Alphabetic Check with Spaces #
main.py
name = "Alice Johnson"
print(name.isalpha())
Output #
False
Explanation #
- Although the string contains only letters and a space,
isalpha()does not allow spaces. - Every character must be an alphabetic letter.
- Therefore, the result is
False.
Example 8: Checking Numeric Input #
main.py
value = "98A7"
print(value.isdigit())
Output #
False
Explanation #
- The string contains the letter
A. - Since not every character is a digit,
isdigit()returnsFalse.
Example 9: Case-Sensitive Prefix Check #
main.py
text = "Python"
print(text.startswith("python"))
Output #
False
Explanation #
startswith()is case-sensitive."Python"and"python"are different strings.- Therefore, the result is
False.
Example 10: Checking the Result Type #
main.py
text = "Python"
result = text.isalpha()
print(result)
print(type(result))
Output #
True
<class 'bool'>
Explanation #
- These string methods return Boolean values.
- The result is either
TrueorFalse. - Therefore, the returned type is
bool.
Common Mistakes #
1. Expecting isalpha() to Allow Spaces #
Incorrect
name = "John Doe"
print(name.isalpha())
Incorrect Expectation #
True
Actual Output #
False
Reason
Spaces are not alphabetic characters.
Correct
print(name.replace(" ", "").isalpha())
2. Assuming isdigit() Accepts Decimal Numbers #
Incorrect
number = "12.5"
print(number.isdigit())
Output #
False
Reason
The decimal point (.) is not a digit.
Correct
number = "125"
print(number.isdigit())
3. Assuming isalnum() Allows Special Characters #
Incorrect
username = "Alice_123"
print(username.isalnum())
Incorrect Expectation #
True
Actual Output #
False
Reason
The underscore (_) is not an alphabetic character or a digit.
4. Forgetting That startswith() Is Case-Sensitive #
Incorrect
text = "Python"
print(text.startswith("python"))
Incorrect Expectation #
True
Actual Output #
False
Reason
String comparisons performed by startswith() are case-sensitive.
Best Practices #
- Use
startswith()andendswith()when validating filenames, URLs, and prefixes. - Use
isalpha()to validate alphabetic input. - Use
isdigit()before converting a numeric string usingint(). - Use
isalnum()when validating usernames or identification codes. - Remember that these methods return Boolean values.
Key Points to Remember #
startswith()checks whether a string begins with a specified substring.endswith()checks whether a string ends with a specified substring.isalpha()returnsTrueonly if all characters are alphabetic.isdigit()returnsTrueonly if all characters are digits.isalnum()returnsTrueonly if all characters are letters or digits.- These methods are case-sensitive where applicable.
- All these methods return values of type
bool.