Python tutorials > Data Structures > Strings > How to check string properties?
How to check string properties?
Introduction to String Property Checks
True
or False
) based on whether the string satisfies the given condition. They are essential for data validation, parsing, and general string manipulation.
Checking if a String is Alphanumeric (.isalnum())
.isalnum()
method checks if all characters in the string are alphanumeric (letters or numbers) and if there is at least one character. If the string contains spaces or special characters, it returns False
.
string1 = "HelloWorld123"
string2 = "Hello World!"
print(string1.isalnum()) # Output: True
print(string2.isalnum()) # Output: False
Checking if a String Contains Only Digits (.isdigit())
.isdigit()
method checks if all characters in the string are digits and if there is at least one character. If the string contains letters or special characters, it returns False
.
string1 = "12345"
string2 = "123abc"
print(string1.isdigit()) # Output: True
print(string2.isdigit()) # Output: False
Checking if a String Contains Only Alphabetic Characters (.isalpha())
.isalpha()
method checks if all characters in the string are alphabetic characters and if there is at least one character. If the string contains numbers or special characters, it returns False
.
string1 = "HelloWorld"
string2 = "Hello123"
print(string1.isalpha()) # Output: True
print(string2.isalpha()) # Output: False
Checking if a String is Lowercase (.islower())
.islower()
method checks if all cased characters in the string are lowercase and if there is at least one cased character. If the string contains uppercase letters or no cased characters, it returns False
.
string1 = "helloworld"
string2 = "HelloWorld"
print(string1.islower()) # Output: True
print(string2.islower()) # Output: False
Checking if a String is Uppercase (.isupper())
.isupper()
method checks if all cased characters in the string are uppercase and if there is at least one cased character. If the string contains lowercase letters or no cased characters, it returns False
.
string1 = "HELLOWORLD"
string2 = "HelloWorld"
print(string1.isupper()) # Output: True
print(string2.isupper()) # Output: False
Checking if a String Starts with a Specific Substring (.startswith())
.startswith()
method checks if the string starts with a specified prefix. It returns True
if the string starts with the prefix, and False
otherwise.
string = "HelloWorld"
print(string.startswith("Hello")) # Output: True
print(string.startswith("World")) # Output: False
Checking if a String Ends with a Specific Substring (.endswith())
.endswith()
method checks if the string ends with a specified suffix. It returns True
if the string ends with the suffix, and False
otherwise.
string = "HelloWorld"
print(string.endswith("World")) # Output: True
print(string.endswith("Hello")) # Output: False
Checking if a String Contains Only Whitespace (.isspace())
.isspace()
method checks if all characters in the string are whitespace characters (spaces, tabs, newlines, etc.) and if there is at least one character.
string1 = " "
string2 = " Hello "
print(string1.isspace()) # Output: True
print(string2.isspace()) # Output: False
Concepts Behind the Snippets
Real-Life Use Case Section
.isalnum()
to validate usernames, .isdigit()
to ensure phone numbers contain only digits, .islower()
to enforce lowercase passwords and .isupper()
to enforce uppercase password requirements. Similarly, you could use .startswith()
to validate email addresses based on domain or organization name and .endswith()
to validate file extensions.
Best Practices
Interview Tip
When to Use Them
Memory Footprint
Alternatives
Pros
Cons
FAQ
-
What happens if I use
.islower()
on a string with no alphabetic characters?
If the string contains no cased characters (e.g., only numbers or symbols),.islower()
returnsFalse
. -
Can I use
.startswith()
or.endswith()
with multiple prefixes/suffixes?
Yes, you can pass a tuple of prefixes/suffixes to.startswith()
or.endswith()
to check against multiple values. -
Are these string property checks case-sensitive?
Yes, methods like.startswith()
and.endswith()
are case-sensitive by default.