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()

Examples

1. Check if given 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."

if str1.isalpha():
    print('Given string contains only alphabets.')
else:
    print('Given string does not contain only alphabets.')
Run Code Copy

Output

Given string does not contain only alphabets.

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

2. Check if string contains only alphabets

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

Python Program

str1 = "HelloWorldPythonExamples"

if str1.isalpha():
    print('Given string contains only alphabets.')
else:
    print('Given string does not contain only alphabets.')
Run Code Copy

Output

Given string contains only alphabets.

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.

Code copied to clipboard successfully 👍