Friday, December 27, 2024
Google search engine
HomeLanguagesPython String isalpha() Method

Python String isalpha() Method

Python String isalpha() method is used to check whether all characters in the String are an alphabet.

Python String isalpha() Method Syntax

Syntax:  string.isalpha()

Parameters: isalpha() does not take any parameters

Returns:

  • True: If all characters in the string are alphabet.
  • False: If the string contains 1 or more non-alphabets.

Errors and Exceptions:

  1. It contains no arguments, therefore an error occurs if a parameter is passed
  2. Both uppercase and lowercase alphabets return “True”
  3. Space is not considered to be the alphabet, therefore it returns “False”

String isalpha() in Python Example

In this example, we have declared a string named as a string and we are checking that string only consists of alphabets.

Python3




string = "Lazyroar"
print(string.isalpha())


Output:

True

Working of isalpha()

In the example, we are checking isalpha () method with three strings. One only with alphabets, second with alphabets and numbers, and third with spaces.

Python3




# checking for alphabets
string = 'Ayush'
print(string.isalpha())
  
string = 'Ayush0212'
print(string.isalpha())
  
# checking if space is an alphabet
string = 'Ayush Saxena'
print( string.isalpha())


Output: 

True
False
False

Practical Application

Given a string in Python, count the number of alphabets in the string and print the alphabets.

Input : string = 'Ayush Saxena'
Output : 11
         AyushSaxena

Input : string = 'Ayush0212'
Output : 5
         Ayush

In the first block of code, the initial string is ‘Ayush Saxena’. The code initializes a counter variable count to zero and two empty strings newstring1 and newstring2. It then iterates through the characters of the string using a for loop, and for each character, it checks whether it is an alphabet using the isalpha() method. If the character is an alphabet, it increments the count variable and appends the character to newstring1. Finally, it prints the count and the new string newstring1.

In the second block of code, the initial string is ‘Ayush0212’. The code performs the same operations as the first block, except that it creates a new string newstring2 to store the alphabets.

Python3




# Given string
string='Ayush Saxena'
count=0
 
# Initialising new strings
newstring1 =""
newstring2 =""
 
# Iterating the string and checking for alphabets
# Incrementing the counter if an alphabet is found
# Finally printing the count
for a in string:
    if (a.isalpha()) == True:
        count+=1
        newstring1+=a
print(count)
print(newstring1)
 
# Given string
string='Ayush0212'
count=0
for a in string:
    if (a.isalpha()) == True:
        count+=1
        newstring2+=a
print(count)
print(newstring2)


Output: 

11
AyushSaxena
5
Ayush

Using isalpha() to Check if a String is Empty

In the case of string1, it is an empty string with no characters at all, so it does not contain any alphabetic characters, and therefore, string1.isalpha() returns False.In the case of string2, it contains a single space character, which is not an alphabetic character, so string2.isalpha() also returns False.

Python3




string1 = ""
string2 = " "
 
print(string1.isalpha())  
print(string2.isalpha())  


Output : 

False
False

Using isalpha() with Unicode Characters

The isalpha() method checks if all the characters in a string are alphabetic characters, i.e., whether they belong to the Unicode category “Letter” (which includes letters from all scripts and alphabets). Therefore, the method will return True if all the characters in the string are letters, and False otherwise.

Python3




string1 = "Hello"
string2 = "مرحبا بالعالم"
string3 = "नमस्ते"
 
print(string1.isalpha()) 
print(string2.isalpha())  
print(string3.isalpha())


Output :

True
False
False

Using isalpha() to check if a String Contains only Letters

In the code snippet above, string1 contains only alphabetic characters, so string1.isalpha() returns True. string2 contains a space character, so string2.isalpha() returns False. string3 contains a digit character, so string3.isalpha() also returns False.

Python3




string1 = "PythonIsFun"
string2 = "Python Is Fun"
string3 = "Python3IsFun"
 
print(string1.isalpha())  
print(string2.isalpha()) 
print(string3.isalpha())  


Output:

True
False
False

 Using isalpha() to Remove non-alphabetic Characters from a String

The code works by iterating through each character in the original text variable and checking if it is an alphabetic character using the isalpha() method. If the character is an alphabetic character, it is added to the alphabetic_text variable.

Python3




text = "Hello, World! How are you today?"
alphabetic_text = ""
for char in text:
    if char.isalpha():
        alphabetic_text += char
print(alphabetic_text)


Output:

HelloWorldHowareyoutoday

RELATED ARTICLES

Most Popular

Recent Comments