如何在 Python 字符串中交替排列元音和辅音?


单词由构成字符串的元音和辅音组成。使用 Python 语言,可以在字符串内部改变元音和辅音。执行此操作的方法可能多种多样,一些方法可能是简单的循环迭代,而另一些方法则可能是一些函数和方法。本文将指导用户学习如何在 Python 字符串中交替排列元音和辅音。本文描述了三种方法。Python 语言中提供了多种内置函数来解决复杂问题。

让我们深入探讨本文。

方法

方法 1:使用 join() 和 zip() 方法

方法 2:使用 lambda 函数

方法 3:使用 zip_longest() 方法

方法 1:使用 join() 和 zip() 方法交替排列元音和辅音的 Python 程序

用于执行字符串元音和辅音交替排列的函数是 join() 和 zip() 方法。列表推导式迭代列表中的字符串。

算法

  • 步骤 1 − 初始化两个空列表,一个用于元音,另一个用于辅音。

  • 步骤 2 − 使用 for 循环从字符串中过滤元音和辅音

  • 步骤 3 − 然后根据字符串中存在的元音和辅音,使用切片方法对其进行更改。

  • 步骤 4 − 新字符串使用 join() 方法将元音和辅音连接在一起。

  • 步骤 5 − 打印语句将返回更改后的字符串。

示例

Open Compiler
#Empty list is initialized to hold the vowels and consonants vow_str = [] con_str = [] #the user is prompted to enter the string str_1 = "Welcome to Paris" #for loop is used to iterate through the string for char in str_1: #checking whether the character of the string is lower if char.isalpha(): if char.lower() in 'aeiou': #append() function is used to add the vowels of the string vow_str.append(char) else: #append() function is used to add the consonants of the string con_str.append(char) #join() method is assigned to the output_str to add both the vowels and consonants output_str = ''.join([a+b for a,b in zip(con_str,vow_str)]) #print statement returns the strings after altering vowels and consonants print(output_str)

输出

WelocemotaPi

方法 2:使用 lambda 函数交替排列元音和辅音的 Python 程序

用于执行字符串元音和辅音交替排列的函数是 join() 和 zip() 方法,以及 lambda 函数的 key 参数。列表推导式迭代列表中的字符串。

算法

  • 步骤 1 − 初始化名为 vowels 的变量。

  • 步骤 2 − 然后根据字符串中存在的元音和辅音,使用 lambda 函数对其进行更改。

  • 步骤 3 − 在 lambda 函数的帮助下过滤元音和辅音。

  • 步骤 4 − 新字符串使用 join() 方法将元音和辅音连接在一起。

  • 步骤 5 − 打印语句将返回更改后的字符串。

示例

#the variable vowel is initialized vowels = 'aeiou' #lambda function filters only the vowels from the string vowel_filter = lambda char: char.lower() in vowels #lambda function filters only the consonants from the string consonant_filter = lambda char: char.lower() not in vowels #the filtered vowels is listed vow_str = list(filter(vowel_filter, str_1)) #the filtered consonants is listed con_str = list(filter(consonant_filter, str_1)) #join() method is assigned to the output_str to add both the vowels and consonants output_str = ''.join([a+b for a,b in zip(con_str,vow_str)]) #print statement returns the strings after altering vowels and consonants print(output_str)

输出

Enter the string: Welcome Home
WelocemoHe

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

方法 3:使用 zip_longest() 函数交替排列元音和辅音的 Python 程序

for 循环用于查找元音和辅音,然后将它们存储在分配的变量中。

算法

  • 步骤 1 − 导入 itertools 模块以使用 zip_longest() 函数。

  • 步骤 2 − 使用 for 循环从字符串中过滤元音和辅音

  • 步骤 3 − 然后根据字符串中存在的元音和辅音对其进行过滤。

  • 步骤 4 − 新字符串使用 join() 方法将元音和辅音连接在一起。

  • 步骤 5 − 打印语句将返回更改后的字符串。

示例

Open Compiler
#importing the itertools function to use the zip_longest function from itertools import zip_longest #the user is prompted to enter the string str_1 = "Welcome home " #the variable vowel is initialized vowels = 'aeiou' #vowel and consonants are assigned to get it from the string vow_str = [char for char in str_1 if char.lower() in vowels] con_str = [char for char in str_1 if char.lower() not in vowels] #join() method is assigned to the output_str to add both the vowels and consonants output_str = ''.join([a+b for a,b in zip_longest(con_str,vow_str, fillvalue='')]) #print statement returns the strings after altering vowels and consonants print(output_str)

输出

Welocemo ehm

结论

Python 语言属于 OOPS 概念,它会立即运行代码而无需检查错误。与其他语言相比,它因其优势而占据独特的地位,例如易于使用简单的语法和语句进行编码。

更新于: 2023-08-25

160 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告