Python string Method - isalpha()

The isalpha() method is a built-in Python string method that returns a boolean value indicating whether all the characters in the string are alphabetic (i.e., either uppercase or lowercase letters).

Here's an example that demonstrates the usage of the isalpha() method:

string1 = "Hello"
string2 = "Hello123"

print(string1.isalpha())  # Output: True
print(string2.isalpha())  # Output: False
Source‮.www:‬theitroad.com

In this example, we create two strings: string1 which contains only alphabetic characters, and string2 which contains a non-alphabetic character. We then call the isalpha() method on each string. The method returns True for string1 since all its characters are alphabetic, and False for string2 since it contains a non-alphabetic character.

The isalpha() method returns True if the string is not empty and all its characters are alphabetic, and False otherwise. Here are some additional examples:

print("".isalpha())  # Output: False
print("123".isalpha())  # Output: False
print("Hello, world!".isalpha())  # Output: False

In the first example, we call the isalpha() method on an empty string, which returns False since the string is not alphabetic. In the second example, we call the method on a string containing only digits, which returns False. In the third example, we call the method on a string that contains non-alphabetic characters, which returns False.