Python – Check if the String contains only Alphabets

Check if String has only Alphabets in Python

To check if a string contains only alphabets, use the function isalpha() on the string. isalpha() returns boolean value. The return value is True if the string contains only alphabets and False if not.

In this tutorial, we will write Python programs to check if the given string contains only alphabets (upper or lower).

Python - Check if String contains only alphabets

Syntax of isalpha()

The syntax of String.isalpha() function is given below.

bool = str1.isalpha()

Example 1: Check if String contains only Alphabets

Let us create a string and check if the string contains only alphabets.

Python Program

str1 = "hello world. welcome to python examples."

bool = str1.isalpha()

print('str1 contains only alphabets:', bool)
Run

Output

str1 contains only alphabets: False

str1 contains two full stops . and some spaces along with alphabets. So isalpha() returned false.

Example 2: Check if String contains only Alphabets

Now, let us check with a string with only alphabets.

Python Program

str1 = "helloworldpythonexamples"

bool = str1.isalpha()

print('str1 contains only alphabets:', bool)
Run

Output

str1 contains only alphabets: True

Summary

In this tutorial of Python Examples, we learned how to check if a string contains only Alphabets or not, with the help of well detailed Python example programs.