In this tutorial, we will look at how to keep only letters (extract alphabets) from a string in Python with the help of examples.
How to extract only alphabets from a string in Python?
You can use a regular expression to extract only letters (alphabets) from a string in Python. You can also iterate over the characters in a string and using the string isalpha()
function to keep only letters in a string.
Let’s look at both the methods with the help of examples –
Extract alphabets from a string using regex
You can use the regular expression 'r[^a-zA-Z]'
to match with non-alphabet characters in the string and replace them with an empty string using the re.sub()
function. The resulting string will contain only letters.
Let’s look at an example.
import re # string with letters, numbers, and special characters s = "BuckyBarnes@123" # keep only letters res = re.sub(r'[^a-zA-Z]', '', s) print(res)
Output:
BuckyBarnes
You can see that the resulting string contains only letters.
Using string isalpha()
function
Alternatively, you can use the string isalpha()
function to remove non-alphabet characters from the string. Use the following steps –
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
- Create an empty string to store our result string with only letters.
- Iterate through each character in our given string.
- For each character, check if its an alphabet using the string
isalpha()
function. If it is, then add the character to our result string.
Let’s look at an example.
# string with letters, numbers, and special characters s = "BuckyBarnes@123" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)
Output:
BuckyBarnes
The result string contains only letters from the original string.
The above code can be reduced to fewer lines using list comprehension.
# string with letters, numbers, and special characters s = "BuckyBarnes@123" # keep only letters res = "".join([ch for ch in s if ch.isalpha()]) print(res)
Output:
BuckyBarnes
We get the same result as above.
You might also be interested in –
- Python – Check If String Contains Only Letters
- Python – Remove Non Alphanumeric Characters from String
- Remove Substring From a String in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.