List comprehension and ord() in Python to remove all characters other than alphabets
Given a string consisting of alphabets and other characters, remove all the characters other than alphabets and print the string so formed.
Input : str = "$Gee*k;s..fo, r'Ge^eks?" Output : GeeksforGeeks
This problem has existing solution please refer Remove all characters other than alphabets from string link.
We will solve this problem in python quickly using List Comprehension.
Approach : is
1. Traverse string 2. Select characters which lie in range of [a-z] or [A-Z] 3. Print them together
How does ord() and range() function works in python ?
- The ord() method returns an integer representing the Unicode code point of the given Unicode character.For example,
ord('5') = 53 and ord('A') = 65 and ord('$') = 36
- The range(a,b,step) function generates a list of elements which ranges from a inclusive to b exclusive with increment/decrement of given step.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course