Explain the following with examples

2. Explain with examples:
i) isalpha()
ii) isalnum()
iii) isspace()
iv) isdcimal
v) istitle

Answer:

String Methods

i) isalpha()

Returns True if all characters in the string are only alphabets (A–Z or a–z).

print("Hello".isalpha())      # True
print("Hello123".isalpha())   # False

ii) isalnum()

Returns True if all characters are alphabets or numbers (no spaces or special characters).

print("Hello123".isalnum())   # True
print("Hello!".isalnum())     # False

iii) isspace()

Returns True if the string has only whitespace characters (like space, tab, newline).

print("   ".isspace())        # True
print("Hello World".isspace()) # False

iv) isdecimal()

Returns True if the string contains only numeric digits (0–9).

print("123".isdecimal())      # True
print("12.3".isdecimal())     # False

v) istitle()

Returns True if each word starts with an uppercase letter followed by lowercase letters (Title Case).

print("Python Is Fun".istitle())    # True
print("python is Fun".istitle())    # False

Leave a Reply

Your email address will not be published. Required fields are marked *