Signup/Sign In
LAST UPDATED: MARCH 31, 2023

How to Extract Alphabets from a given String in Python

Technology

    This lesson will teach you how to use Python's ord(char) and chr.isalpha functions to extract alphabets from a given string. These techniques are frequently used for string manipulation and can assist you in isolating particular characters within a string. You will have a greater grasp of how to extract characters and implement these methods in your own Python programs by the conclusion of this lesson. So, let's get started and investigate these techniques!


    Using ord(char)

    • Get the input from the user using the input() method.
    • Declare an empty string to store the alphabets.
    • Loop through the string:
      • If the ASCII value of char is between 65 and 90 or 97 and 122. Use the ord() method for the ASCII values of chars.
        • Add it to the empty string
    • Print the resultant string.
    ## getting the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to apppend only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## ord(chr) returns the ascii value
    ## CHECKING FOR UPPER CASE
    if ord(char) >= 65 and ord(char) <= 90:
    only_alpha += char
    ## checking for lower case
    elif ord(char) >= 97 and ord(char) <= 122:
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)

    Input:

    Enter a string: study123tonight

    Output of the program:

    studytonight


    Using chr.isalpha()

    • Get the input string from the user using the input() method.
    • Declare an empty string to store the alphabets.
    • Loop through the string:
      • Check whether the char is an alphabet or not using chr.isalpha() method.
        • Add it to the empty string.
    • Print the resultant string.
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)

    Input:

    Enter a string: study123tonight

    Output of the program:

    studytonight

    If you have any queries regarding the programs, please let me know in the comment section below.

    Conclusion

    We learned how to extract alphabets from a given string in Python using two distinct techniques in this lesson. These methods can be used to perform a variety of string manipulation jobs in Python, making them valuable tools for any coder. You can increase your Python abilities and ability to work with strings by learning and understanding these techniques.

    Frequently Asked Questions(FAQs)

    1. What is string manipulation in Python?

    String manipulation refers to modifying and extracting specific characters or substrings from a given string using built-in functions and methods.

    2. How do you extract alphabets from a string in Python?

    You can use the ord(char) method or the chr.isalpha() method to extract only alphabets from a given string in Python.

    3. What is the ord(char) method in Python?

    The ord(char) method is a built-in function in Python that returns the Unicode value of a given character. It can be used to extract alphabets from a string by checking the Unicode range.

    4. What is the chr.isalpha() method in Python?

    The chr.isalpha() method is a built-in method in Python that checks whether a given character is an alphabet or not. It can be used to extract only alphabets from a string by iterating through each character and checking its type.

    4. How to extract alphabets from a string in Java?

    • charAt(): You can use charAt() method to extract a single character from a string.
    • toCharArray(): You can convert all the characters of any array into a character array.

    You may also like:

    I am a Computer Science Student. I am interested in Programming. I love to know about new technologies.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS