Check if a string is only letters python
Contents Show
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).
Syntax of isalpha()The syntax of String.isalpha() function is given below.
Example 1: Check if String contains only AlphabetsLet us create a string and check if the string contains only alphabets. Python Program
Run Output
str1 contains two full stops Example 2: Check if String contains only AlphabetsNow, let us check with a string with only alphabets. Python Program
Run Output
SummaryIn 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. Related Tutorials
In this tutorial, we will look at how to check whether a string contains only letters or not in Python with the help of examples. How to check if a string contains only alphabets in Python?You can use the string # check if string s contains only letters s.isalpha() It returns Let’s look at an example. # create a string s = "Bucky" # check if string contains only alphabets print(s.isalpha()) Output: True We get Let’s look at another example. # create a string s = "Bucky7" # check if string contains only alphabets print(s.isalpha()) Output: False Here, we get Also, be careful if you’re string contains any punctuations. For example, for the string “you’re” the # string with punctuations s = "you're" # check if string contains only alphabets print(s.isalpha()) Output: False This is because not all characters in the string are alphabets. The character Again, this is expected because the objective of the isalpha() function is to check if all the characters are alphabets or not. For more on the string isalpha() function, refer to its documentation. You might also be interested in –
How do you check if a string contains only letters in Python?Use str. isalpha() to check if a string contains only letters
isalpha() with str as the string to be checked to return True if all characters in str are alphabetic and False otherwise.
How do you check if a string has only letters?Use the test() method to check if a string contains only letters, e.g. /^[a-zA-Z]+$/. test(str) . The test method will return true if the string contains only letters and false otherwise.
|