Is string an alphabet python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python String isalpha() method is used to check whether all characters in the String is an alphabet.

    Python String isalpha() Method Syntax:

    Syntax:  string.isalpha()

    Parameters: isalpha() does not take any parameters

    Returns:

    • True: If all characters in the string are alphabet.
    • False: If the string contains 1 or more non-alphabets.

    Errors and Exceptions:

    1. It contains no arguments, therefore an error occurs if a parameter is passed
    2. Both uppercase and lowercase alphabets return “True”
    3. Space is not considered to be the alphabet, therefore it returns “False”

    Python String isalpha() Method Example:

    Python3

    string = "geeks"

    print(string.isalpha())

    Output:

    True

    Example 1: Working of isalpha()

    Python3

    string = 'Ayush'

    print(string.isalpha())

    string = 'Ayush0212'

    print(string.isalpha())

    string = 'Ayush Saxena'

    print( string.isalpha())

    Output: 

    True
    False
    False

    Example 2: Practical Application

    Given a string in Python, count the number of alphabets in the string and print the alphabets.

    Input : string = 'Ayush Saxena'
    Output : 11
             AyushSaxena
    
    Input : string = 'Ayush0212'
    Output : 5
             Ayush

    Algorithm:

    1. Initialize a new string and variable counter to 0. 
    2. Traverse the given string character by character up to its length, check if the character is an alphabet. 
    3. If it is an alphabet, increment the counter by 1 and add it to a new string, else traverse to the next character. 
    4. Print the value of the counter and the new string.

    Python3

    string='Ayush Saxena'

    count=0

    newstring1 =""

    newstring2 =""

    for a in string:

        if (a.isalpha()) == True:

            count+=1

            newstring1+=a

    print(count)

    print(newstring1)

    string='Ayush0212'

    count=0

    for a in string:

        if (a.isalpha()) == True:

            count+=1

            newstring2+=a

    print(count)

    print(newstring2)

    Output: 

    11
    AyushSaxena
    5
    Ayush

    Time Complexity: O(n)

    Auxiliary Space: O(n)


    ❮ String Methods


    Example

    Check if all the characters in the text are letters:

    txt = "CompanyX"

    x = txt.isalpha()

    print(x)

    Try it Yourself »


    Definition and Usage

    The isalpha() method returns True if all the characters are alphabet letters (a-z).

    Example of characters that are not alphabet letters: (space)!#%&? etc.


    Syntax

    Parameter Values

    No parameters.


    More Examples

    Example

    Check if all the characters in the text is alphabetic:

    txt = "Company10"

    x = txt.isalpha()

    print(x)

    Try it Yourself »


    ❮ String Methods


    What is a string in Python?

    String is a collection of alphabets, words or other characters. It is one of the primitive data structures and are the building blocks for data manipulation. Python has a built-in string class named str . Python strings are "immutable" which means they cannot be changed after they are created.

    Is Python a numeric or alphabet?

    The Python isalpha() method returns true if a string only contains letters. Python isnumeric() returns true if all characters in a string are numbers.

    Is text a string in Python?

    This page is about the Python string, which is the go-to Python data type for storing and using text in Python. So, in Python, a piece of text is called a string, and you can perform all kinds of operations on a string.

    How do you tell if a character is a letter in Python?

    Use str..
    for character in a_string: Iterate through each character of `a_string`.
    is_letter = character. isalpha() Check if `character` is a letter..
    print(character, is_letter).