To check if a string only contains letters in Python, you can use the string isalpha() function.

a = "hello1"
b = "bye"
c = "123"

print(a.isalpha())
print(b.isalpha())
print(c.isalpha())

#Output:
False
True
False

When working with strings in Python, the ability to check these strings for certain conditions is very valuable.

One such case is if you want to check if a string variable contains only letters or not.

To check if a string variable only contains letters, we can use the string isalpha() function.

isalpha() returns True if all characters of the string are letters.

Below is an example showing you how to check if a string is made up of all letters in Python with isalpha().

a = "hello1"
b = "bye"
c = "123"

print(a.isalpha())
print(b.isalpha())
print(c.isalpha())

#Output:
False
True
False

Hopefully this article has been useful for you to check if a string only contains letters.

Categorized in:

Python,

Last Update: March 22, 2024