Hướng dẫn regex remove vowels python
I know there are many correct solutions on this subject but I thought to add few fun ways of solving this problem. If you come from a C++/C# or Java, you will tend to use something like compare then action using the index to remove the unwanted entry in a for loop. Python has the Remove and Del functions. Remove function uses the value and del uses the index.The pythonic solution is in the last function. Lets see how we can do that: Show Here we are using the index in a for loop and del function very similar in C++:
Using the remove function:
Building new string that does not contain the unwanted chars using their indexes:
Same as in the solution in above but using the value:
How you should do it in python? Using list comprehension! It is beautiful: I know there are many correct solutions on this subject but I thought to add few fun ways of solving this problem. If you come from a C++/C# or Java, you will tend to use something like compare then action using the index to remove the unwanted entry in a for loop. Python has the Remove and Del functions. Remove function uses the value and del uses the index.The pythonic solution is in the last function. Lets see how we can do that: Building new string that does not contain the unwanted chars using their indexes: View Discussion Improve Article Save Article View Discussion Improve Article Save Article Given a string, remove the vowels from the string and print the string without vowels. Examples: Input : welcome to geeksforgeeks Output : wlcm t gksfrgks Input : what is your name ? Output : wht s yr nm ? A loop is designed that goes through a list composed of the characters of that string, removes the vowels and then joins them. Implementation: C++14
Java
Python3
C#
Javascript
Output GksfrGks - Cmptr Scnc Prtl fr Gks Time Complexity: O(n), where n is the length of the string We can improve the above solution by using Regular Expressions. Implementation: C++
Java
Python3
C#
Output GksfrGks - Cmptr Scnc Prtl fr Gks Time Complexity: O(n), where n is the length of the string This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Using translate(): translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "" . In Python you can use the replace() and translate() methods to specify which characters you want to remove from a string and return a new modified string result. It is important to remember that the original string will not be altered because strings are immutable. Approach: Iterate string using a loop and check for the repetitiveness of vowels in a given sentence and in case if consecutive vowels are found then delete the vowel till coming next consonant and printing the updated string. |