Open In App

Python String isalpha() Method

Last Updated : 09 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 = "geeks"
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


Previous Article
Next Article

Similar Reads

Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
Some of the string methods are covered in the set 3 below String Methods Part- 1 More methods are discussed in this article 1. len() :- This function returns the length of the string. 2. count("string", beg, end) :- This function counts the occurrence of mentioned substring in whole string. This function takes 3 arguments, substring, beginning posi
4 min read
numpy string operations | isalpha() function
numpy.core.defchararray.isalpha(arr) function returns True for each element if all characters in the string are alphabetic and there is at least one character; returns false otherwise. Parameters: arr : array_like of str or unicode. Returns : [ndarray] Output array of bools. Code #1 : # Python program explaining # numpy.char.isalpha() method import
1 min read
Python | Pandas Series.str.isalpha()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas str.isalpha() method is used to check if all characters in each string in series are alphabetic(a-z/A-Z). Whitespace or any other
2 min read
Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
String slicing in Python to check if a string can become empty by recursive deletion
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again. Examples: Input : str = "GEEGEEKSKS", sub_str = "GEEKS" Output : Yes Explanation :
2 min read
String slicing in Python to Rotate a String
Given a string of size n, write functions to perform following operations on string. Left (Or anticlockwise) rotate the given string by d elements (where d <= n).Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeek
3 min read
Python | Check if given string can be formed by concatenating string elements of list
Given a string 'str' and a list of string elements, write a Python program to check whether given string can be formed by concatenating the string elements of list or not. Examples: Input : str = 'python' lst = ['co', 'de', 'py', 'ks', 'on'] Output : False Input : str = 'geeks' lst = ['for', 'ge', 'abc', 'ks', 'e', 'xyz'] Output : True Approach #1
5 min read
Python | Check if string ends with any string in given list
While working with strings, their prefixes and suffix play an important role in making any decision. For data manipulation tasks, we may need to sometimes, check if a string ends with any of the matching strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using filter() + endswith() The combination of the above func
6 min read
Python | Sorting string using order defined by another string
Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once. Examples: Input : pat = "asbcklfdmegnot", str = "eksge" Output : str = "g
2 min read
Python | Merge Tuple String List values to String
Sometimes, while working with records, we can have a problem in which any element of record can be of type string but mistakenly processed as list of characters. This can be a problem while working with a lot of data. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension + join() The combination of abov
6 min read
Practice Tags :
three90RightbarBannerImg