

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete consonants from a string in Python?
It is easiest to use regular expressions for this problem. You can separate multiple characters by "|" and use the re.sub(chars_to_replace, string_to_replace_with, str). We have For example:
>>> import re >>> consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'] >>> re.sub('|'.join(consonants), "", "Hello people", flags=re.IGNORECASE) "eo eoe"
Note: You can also use the [] to create group of characters to replace in regex.
If you want to keep only vowels and remove all other characters, you can use an easier version. Note that it'll remove spaces, numbers, etc. as well. For example,
>>> import re >>> re.sub('[^aeiou]', "", "Hello people", flags=re.IGNORECASE) "eoeoe"
You can also filter out the consonants as follows:
>>> consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'] >>> s = "Hello people" >>> ''.join(c for c in s if c.lower() not in consonants) 'eo eoe'
- Related Questions & Answers
- How to delete a character from a string using python?
- Reversing consonants only from a string in JavaScript
- How to remove consonants from a string using regular expressions in Java?
- How to find consonants in a given string using Java?
- How to delete lines from a Python tkinter canvas?
- Python Pandas - How to delete a row from a DataFrame
- Delete all whitespaces from a String in Java
- How to detect vowels vs consonants in Python?
- How to delete the vowels from a given string using C language?
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- Java program to delete duplicate characters from a given String
- How to count number of vowels and consonants in a string in C Language?
- C# Program to count number of Vowels and Consonants in a string
- How can you delete a table from a database in MySQL Python?
- How to delete a table from MongoDB database?
Advertisements