Which string method is used to check whether given character is letter or a number?
The syntax of Show
string.isalpha() isalpha() Parameters
Return Value from isalpha()The
Example 1: Working of isalpha()
Output True False False Example 1: Working of isalpha()
Output All characters are alphabets Checkout these related String methods as well:
In this Python tutorial, we will learn to check if a character in a string is a letter using Python programming language. For this, we will have a string in which we will have some string and a integer. Then we will look for letters in that string. Table Of Contents
Method 1 : Using isalpha() methodFirst method that we will be using is the isalpha() method of string class. It returns True if all charaters are alphabets and return False otherwise. SYNTAX : string.isalpha() Advertisements Here, first we will check for whole string variable whether it has integers inside it or has only alphabets/letters. It will return True if the given string has letters only. If the given string has both characters/letters and integers then it will return False. See the example code below, EXAMPLE : # initialized a string with integer and letters str_var = '312341AFc' # returns true if string has only letters else returns false print(str_var.isalpha()) OUTPUT : False Now, to iterate and check for characters inside the string, we will be using for loop with isalpha() method to check if each character in the string is a letters or not. See the Example code below EXAMPLE : # initialized a string with integer and letters str_var = '312341AFc' # iterating over the characters of the string # to check if it has letters or not. for char in str_var: result = char.isalpha() print(char, ':', result) OUTPUT : 3 : False 1 : False 2 : False 3 : False 4 : False 1 : False A : True F : True c : True In the code and output above, we have used the isalpha() method to check if the character in a string is a letter or not. First we checked the whole string then we iterated over the string variable and checked for the each char, if it is letter or not. It prints true in front of character which determines that it is a letter. Method 2 : Checking using ASCII values and ord() functionNow in this method, we will be using the ASCII values and ord() function to check if character in a string is a letter. But first what is ASCII Value and ord() function? ASCII Value :: ASCII Stands for American Standard Code for Information Interchange, and it is the most used character encoding format.It is a 7-bit character code in which every single bit represents a unique character. Every character in english alphabet has a unique ASCII code.
ord() function : The ord() function is a built in function in Python programming language, which takes a unicode character as an argument and returns the ASCII code of that character. Here what we can do is, loop through all the characters in the given string, and using ord() fucntion check if the ASCII value of the each character lies between 65 to 90 or between 97 to 122. See the example code below, EXAMPLE : # initialized a string with integer and letters str_var = "13A24K3243253434s59w459" # iterating through str_var for x in str_var: # checking for upper case alphabets if ord(x) >= 65 and 90 >= ord(x): print('Found letter', x) # checking for lower case alphabets elif ord(x) >= 97 and 122 >= ord(x): print('Found letter', x) OUTPUT : Found letter A Found letter K Found letter s Found letter w In the code and output above, using the ord() function we successfully found out the letters in the string str_var. In this method we fetched the ASCII value of each character in string using order() function and then checked if they are letters or not. SummaryIn this Python tutorial, we used two different methods to check if a character in a string is a letter using Python Programming Language. You can always use any of the methods depending upon your requirement but the most used and easy to understand and also has the shorter syntax is method 1 which is by using isalpha() , method 2 also can be very useful because through which you can find for special characters in the given string but you need to know about ASCII values. Also we have used Python 3.10.1 for writing examples. Type python –version to check your python version. Always try to read, write & understand example codes. Happy Coding. Which string method is used to count the number of characters in a string?Python String count() Method
The count() method returns the number of times a specified value appears in the string.
Which string method returns true when the string contains only letters and numbers?The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9). Example of characters that are not alphanumeric: (space)! #%&? etc.
How can you tell if a character is a string or integer?Using built-in method isdigit(), each character of string is checked. If the string character is a number, it will print that string contains int. If string contains character or alphabet, it will print that string does not contain int.
|