Validation Methods in Python

Python provides several validation (testing) methods that help us check the content of strings. These methods return either True or False based on whether the string satisfies a certain condition like being alphabetic, numeric, lowercase, uppercase, etc.

1. isalpha() Method

Returns True if all characters in the string are alphabetic (A-Z or a-z), otherwise False.

Syntax:

string.isalpha()

Example:

text1 = "Hello"
text2 = "Hello123"
print(text1.isalpha())
print(text2.isalpha())

Output:

True
False

Explanation:

  • text1 contains only letters, so it returns True.
  • text2 has numbers, so it returns False.

2. isdigit() Method

Returns True if all characters are digits (0–9), otherwise False.

Syntax:

string.isdigit()

Example:

text1 = "123"
text2 = "123abc"
print(text1.isdigit())
print(text2.isdigit())

Output:

True
False

Explanation:

  • text1 has only numbers, so it returns True.
  • text2 has letters, so it returns False.

3. isalnum() Method

Returns True if all characters are alphanumeric (letters or digits), and at least one character exists.

Syntax:

string.isalnum()

Example:

text1 = "Python3"
text2 = "Python!"
print(text1.isalnum())
print(text2.isalnum())

Output:

True
False

Explanation:

  • text1 has letters and numbers, valid alphanumeric.
  • text2 includes ! which is not alphanumeric.

4. isspace() Method

Returns True if the string contains only whitespace characters (like spaces, tabs), otherwise False.

Syntax:

string.isspace()

Example:

text1 = "   "
text2 = "  a  "
print(text1.isspace())
print(text2.isspace())

Output:

True
False

Explanation:

  • text1 has only spaces, so it returns True.
  • text2 has a letter a, so returns False.

5. isupper() Method

The isupper() method returns True if:

  • The string is not empty.
  • There is at least one alphabetic character.
  • All alphabetic characters in the string are in uppercase.

Any non-alphabetic characters (like numbers, spaces, symbols) are ignored while checking.

Syntax:

string.isupper()

Example:

text1 = "PYTHON"
text2 = "PYTHON123"
text3 = "Python"
text4 = "123@#"
text5 = ""

print(text1.isupper())   # True
print(text2.isupper())   # True
print(text3.isupper())   # False
print(text4.isupper())   # False
print(text5.isupper())   # False

Output:

True
True
False
False
False

Explanation:

  • "PYTHON" → All letters are uppercase → True
  • "PYTHON123" → Letters are uppercase, numbers ignored → True
  • "Python" → P is uppercase but others are lowercase → False
  • "123@#" → No alphabetic character → False
  • "" → Empty string → False

6. islower() Method

The islower() method returns True if:

  • The string is not empty.
  • There is at least one alphabetic character.
  • All alphabetic characters in the string are in lowercase.

Any non-alphabetic characters (like numbers, spaces, symbols) are ignored while checking.

Syntax:

string.islower()

Example:

text1 = "python"
text2 = "python123"
text5 = ""

print(text1.islower())   # True
print(text2.islower())   # True
print(text5.islower())   # False

Output:

True
True
False

Explanation:

  • "python" → All letters are lowercase → True
  • "python123" → Letters are lowercase, numbers are ignored → True
  • "" → Empty string → False

7. istitle() Method

Returns True if the string follows title case (first letter of each word is capital), otherwise False.

Syntax:

string.istitle()

Example:

text1 = "Python Is Fun"
text2 = "python Is Fun"
print(text1.istitle())
print(text2.istitle())

Output:

True
False

Explanation:

  • text1 has each word starting with uppercase.
  • text2 starts with lowercase, so returns False.

8. startswith() Method

The startswith() method checks whether a string starts with a specific prefix. It is case-sensitive means A and a are considered different. It returns True if the string starts with the given value; otherwise, it returns False.

Syntax:

string.startswith(prefix, start, end)

Parameters:

  • prefix – The string or tuple of strings to check at the start.
  • start (optional) – The position to start the search.
  • end (optional) – The position to end the search.

Example:

text = "python programming"
result1 = text.startswith("py")
result2 = text.startswith("gram", 7)
print(result1)
print(result2)

Output:

True
True

Explanation:

  • "python programming" starts with "py" → True
  • From index 7 onward, string is "gramming", which starts with "gram" → True

9. endswith() Method

The endswith() method checks whether a string ends with a specific suffix. It is case-sensitive means A and a are considered different. It returns True if the string ends with the given value; otherwise, it returns False.

Syntax:

string.endswith(suffix, start, end)

Parameters:

  • suffix – The string or tuple of strings to check at the end.
  • start (optional) – The position to start the search.
  • end (optional) – The position to end the search.

Example:

text = "python programming"
result1 = text.endswith("ing")
result2 = text.endswith("thon", 0, 6)
print(result1)
print(result2)

True
True

Explanation:

  • "python programming" ends with "ing" → True
  • text[0:6] is "python", which ends with "thon" → True

String Validation Methods – Summary Table:

Method Returns True if...
isalpha() All characters are alphabets (A–Z or a–z) and the string is not empty.
isdigit() All characters are digits (0–9) and the string is not empty.
isalnum() All characters are either alphabets or digits (no special chars), and not empty.
isspace() All characters are whitespace (spaces, tabs, newlines) and not empty.
isupper() All alphabetic characters are uppercase and string is not empty.
islower() All alphabetic characters are lowercase and string is not empty.
istitle() Each word starts with an uppercase letter followed by lowercase letters.
startswith("x") The string starts with the specified substring.
endswith("x") The string ends with the specified substring.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar