AppDividend
Latest Code Tutorials

Python String isalpha() Method Example

0

Python String isalpha() method in python is used to check whether the given string consists of only alphabets. In other words, we can say that it is a method of handling strings. Python isalpha() returns True if every character of the string is an alphabet and returns False otherwise.

Python String isalpha()

If you want to check if the string contains only the alphabet, then Python isalpha() function solves your problem. See the following syntax.

Syntax

string.isalpha()

It doesn’t take any parameters. There will error shown if we try to pass any parameter to the method.

The isalpha() function returns true if the string consists only of alphabets. (Both uppercase and lowercase)

Python string isalpha() function returns False if the string doesn’t contain alphabets or contains characters other than alphabets like numerical or special characters. When it identifies a space, then also it returns False.

Algorithm

1. Initialize the new string and variable counter to 0.
2. Traverse the given string character by character up to its length, check if the character is an alphabet.
3. If it is an alphabet, increment the counter by 1 and add it to the new string, else traverse to the next character.
4. Print a value of the counter and the new string.

Example programs on isalpha() method

Write a program to show the mechanism of isalpha()
# app.py

string = "HelloBoy"
string2 = "Hello Boy"
print("This string will return true as it contains only alphabets:")
print("String=", string)
print(string.isalpha())
print("This string will return false as it contains alphabets and one space:")
print("String=", string2)
print(string2.isalpha())

Output

➜  pyt python3 app.py
This string will return true as it contains only alphabets:
String= HelloBoy
True
This string will return false as it contains alphabets and one space:
String= Hello Boy
False
➜  pyt
Write a program to count the alphabets of the string.
# app.py

string = "Hello World"
count = 0
count1 = 0
for i in string:
    if(i.isalpha()) == True:
        count = count+1
for i in string:
    count1 = count1+1
print("String: ", string)
print("Length of the string including space: ", count1)
print("Length of the string just by counting alphabets: ", count)

Output

➜  pyt clear
➜  pyt python3 app.py
String:  Hello World
Length of the string including space:  11
Length of the string just by counting alphabets:  10
➜  pyt

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”.

Finally, Python String isalpha() Method Example is over.

Related Posts

Python string isalnum()

Python string encode()

Python string isdecimal()

Python string isdigit()

Python string to list

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.