我是Python的初学者。到目前为止我得到了这个:
vowels = 'aeiouAEIUO'
consonants = 'bcdfghjklmnñpqrstvwxyzBCDFGHJKLMNÑPQRSTVWXYZ'
a = input('Type a word: ')
for i in range(len(a)):
cont1 = 0
cont2 = 0
if a[i] in consonants:
cont1 = i - 1
cont2 = i + 1
if a [cont1] in vowels and a [cont2] in vowels:
print('consonant between vowels')
我想分开音节。我想创建一个规则,当辅音在2个元音之间时,辅音必须加到右边的元音上并打印由' - '分隔的音节 montoya = mon-to-ya amor = a-mor clase = cla-se
这段代码当然会出错:IndexError:字符串索引超出范围。我不知道这意味着什么。
由于
答案 0 :(得分:0)
我想创建一个规则,当辅音在2个元音之间时, 辅音必须加入到右边的元音上。
您可以使用re
:
>>> import re
>>> vowels = 'aeiouAEIUO'
>>> consonants = 'bcdfghjklmnñpqrstvwxyzBCDFGHJKLMNÑPQRSTVWXYZ'
>>> pattern="([" + vowels + "])" + "([" + consonants + "]" + "[" + vowels + "])"
>>> re.sub(pattern, r'\1-\2', "amor")
'a-mor'
>>> re.sub(pattern, r'\1-\2', "clase")
'cla-se'
>>>