python中字符串模块_使用Python中的re模块,从给定的字符串中删除所有辅音

python中字符串模块

To solve this type of problem, we will use the re module or regular expression module in the Python. Here, we will assume a string given by the user from which we have to remove all consonants that is we have to find all vowels present in the given strings. So, before going to solve this problem we will learn a little bit about re module.

为了解决这类问题,我们将在Python中使用re模块正则表达式模块 。 在这里,我们假设用户给定的字符串,我们必须从中删除所有辅音,即必须找到给定字符串中存在的所有元音。 因此,在解决此问题之前,我们将学习一些有关re模块的知识。

什么是Python中的re模块? (What is re module in the Python?)

Python has an inbuilt module called re which allows us to solve the various problems based on pattern matching and string manipulation.

Python有一个名为re的内置模块,它使我们能够基于模式匹配和字符串处理来解决各种问题。

Let us assume a string s is given by the user it has a mixture of consonants and vowels. Here, we are going to write Python code that will remove all consonants from the string s.

让我们假设用户给定的字符串s混合了辅音和元音。 在这里,我们将编写Python代码,该代码将从字符串 s中 删除所有辅音

    s = 'Includehelp is specially designed to provide help to students, working professionals and job seekers. We are fully dedicated to making each tutorial very simple to learn and understand.'

Code:

码:

# importing the module
import re

# string initialization
s ='Includehelp is specially designed to provide help to students, working professionals and job seekers. We are fully dedicated to making each tutorial very simple to learn and understand.'

# removing all constants
s1=re.sub('[^aeiouAEIOU ]+','',s)

# printing the string after removing all constants
print(s1)

Output

输出量

Iuee i eia eie o oie e o ue oi oeioa a o eee e ae u eiae o ai ea uoia e ie o ea a uea

Explanation:

说明:

  • Here, we have included the re module in the Python code by using the import function.

    在这里,我们通过使用import函数将re模块包含在Python代码中。

  • In re module, re.sub use to replace the substring. The syntax of re.sub() is re.sub(Pattern, replace, string) and in the above code, we are replacing all letters with '' (empty space) excepting the vowel "aeiouAEIOU".

    re module中re.sub用于替换子字符串。 re.sub()的语法是re.sub(Pattern,replace,string) ,在上面的代码中,我们将所有字母替换为'' (空白),但元音“ aeiouAEIOU”除外。

  • At the end of the program, we are printing the left string after the replacement of all consonants and this is our requirement.

    在程序结束时,我们将在替换所有辅音后打印左字符串,这是我们的要求。

翻译自: https://www.includehelp.com/python/remove-all-consonants-from-the-given-string-by-using-the-re-module-in-python.aspx

python中字符串模块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值