I'm trying to check if a string only contains letters, not digits or symbols. Show For example:
Tomerikoo 16.6k15 gold badges37 silver badges54 bronze badges asked Sep 6, 2013 at 22:17
0 Simple:
Demo:
answered Sep 6, 2013 at 22:22
Martijn Pieters♦Martijn Pieters 985k273 gold badges3876 silver badges3235 bronze badges 7 The
answered Sep 6, 2013 at 22:21
cmdcmd 5,61415 silver badges29 bronze badges 2 For people finding this question via Google who might want to know if a string contains only a subset of all letters, I recommend using regexes:
answered Mar 20, 2015 at 13:25
Martin ThomaMartin Thoma 112k148 gold badges568 silver badges873 bronze badges 1 You can leverage regular expressions.
The An easier approach is to use the
answered Jan 7, 2019 at 7:30
ShailShail 8319 gold badges20 silver badges36 bronze badges Actually, we're now in globalized world of 21st century and people no longer communicate using ASCII only so when anwering question about "is it letters only" you need to take into account letters from non-ASCII alphabets as well. Python has a pretty cool unicodedata library which among other things allows categorization of Unicode characters:
The categories and their abbreviations are defined in the Unicode standard. From here you can quite easily you can come up with a function like this:
And then:
As you can see the whitelisted categories can be quite easily controlled by the tuple inside the function. See this article for a more detailed discussion. answered Jun 12, 2017 at 9:49
kravietzkravietz 10k2 gold badges31 silver badges27 bronze badges answered Sep 6, 2013 at 22:24
Looks like people are saying to use
This is the one line function to check if all characters are letters.
More generally,
answered Dec 18, 2015 at 19:52
hlin117hlin117 19.2k29 gold badges69 silver badges91 bronze badges 2 (1) Use str.isalpha() when you print the string. (2) Please check below program for your reference:-
Note:- I checked above example in Ubuntu. answered Jan 2, 2018 at 6:50
A pretty simple solution I came up with: (Python 3)
You can add a space in the string you are checking against if you want spaces to be allowed.
Anonymous 6994 gold badges15 silver badges34 bronze badges answered Nov 12, 2015 at 14:45
4 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.
How do you check if string contains only letters and numbers Python?isalnum() is a built-in Python function that checks whether all characters in a string are alphanumeric. In other words, isalnum() checks whether a string contains only letters or numbers or both. If all characters are alphanumeric, isalnum() returns the value True ; otherwise, the method returns the value False .
How do you check if a string contains only alphabets and spaces in Python?Method #1 : Using all() + isspace() + isalpha()
This is one of the way in which this task can be performed. In this, we compare the string for all elements being alphabets or space only.
How do you check if the string contains only letters or digits?To check whether a String contains only unicode letters or digits in Java, we use the isLetterOrDigit() method and charAt() method with decision-making statements. The isLetterOrDigit(char ch) method determines whether the specific character (Unicode ch) is either a letter or a digit.
|