我正在尝试使用python编写一个函数来检查给定单词的第一个字母,例如“ball”是大写还是小写的元音。例如:
#here is a variable containing a word:
my_word = "Acrobat"
#letters in vowel as a list
the_vowel = ["a","e","i","o","u"]
如何检查“Acrobat”中的第一个字母是列表中的元音之一?我还需要考虑它是大写还是小写?
尝试my_word[0].lower() in the_vowel
我不知道它是否比这里已经发布的答案更好,但你也可以这样做:
vowels = ('a','e','i','o','u','A','E','I','O','U')
myWord.startswith(vowels)
这里有一些提示可以帮助您弄清楚。
从字符串中获取单个字母的下标字符串。
>>> 'abcd'[2]
'c'
请注意,第一个字符是字符零,第二个字符是字符一,依此类推。
接下来要注意的是,大写字母不等于小写字母:
>>> 'a' == 'A'
False
upper
幸运的是,python 字符串具有lower
更改字符串大小写的方法:
>>> 'abc'.upper()
'ABC'
>>> 'a' == 'A'.lower()
True
要测试我们列表中的成员资格in
:
>>> 3 in [1, 2, 3]
True
>>> 8 in [1, 2, 3]
False
因此,为了解决您的问题,将下标绑定在一起以获得单个字母, upper
/lower
调整大小写,并使用in
.
my_word = "Acrobat"
the_vowel = "aeiou"
if myword[0].lower() in the_vowel:
print('1st letter is a vowel')
else:
print('Not vowel')
我的代码看起来像这样。
original = raw_input("Enter a word:")
word = original.lower()
first = word[0]
vowel = "aeiou"
if len(original) > 0 and original.isalpha():
if first in vowel:
print word
print first
print "vowel!"
else:
print word
print first
print "consonant
x = (input ("Enter any word: "))
vowel = "aeiouAEIOU"
if x[0] in vowel:
print ("1st letter is vowel: ",x)
else:
print ("1st letter is consonant: ",x)
这是我的做法,因为在将输入的单词存储为变量之前需要先检查它:
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
if first in ['a','e','i','o','u']:
print "vowel"
else:
print "consonant"
else:
print 'empty'
这是 codecadmy.com 上练习的解决方案:
original = raw_input('Enter a word:')
word = original.lower()
first = word[0]
vowel = "aeiou"
if len(original) > 0 and original.isalpha():
if first in vowel:
print 'vowel'
else:
print 'consonant'
else:
print 'empty'
变化:
if my_word[0] in ('a','e','i','o','u'):
print(' Yes, the first letter is vowel ')
else:
print(' No, the first letter is not vowel ')
所以,这是找出第一个字母是元音还是不是元音的简单代码!如果您在 python 或 js 中有任何进一步的查询,请将其注释掉。
import ast,sys
input_str = sys.stdin.read()
if input_str[0] in ['a','e','i','o','u','A','E','I','O','U']:
print('YES')
else:
print('NO')
将 the_vowel 定义为字典会比列表快吗?
the_vowel = {"a":1,"e":1,"i":1,"o":1,"u":1}
my_word[0].lower() in the_vowel
反元音功能
def anti_vowel(text):
vowel = ["a","e","i","o","u"]
new_text = ''
for char in text:
if char.lower() in vowel:
continue
else:
new_text += char
print new_text
return new_text
x = raw_input("Enter a word: ")
vowels=['a','e','i','o','u']
for vowel in vowels:
if vowel in x:
print "Vowels"
else:
print "No vowels"
这将打印出 5 行,如果其中任何一行包含表示元音的行,则表示存在元音。我知道这不是最好的方法,但它确实有效。
inp = input('Enter a name to check if it starts with vowel : ') *# Here we ask for a word*
vowel = ['A','E','I','O','U', 'a','e','i','o','u'] *# This is the list of all vowels*
if inp[0] in vowel:
print('YES') *# Here with the if statement we check if the first alphabet is a vowel (alphabet from the list vowel)*
else:
print('NO') *# Here we get the response as NO if the first alphabet is not a vowel*
让我们以更简单的方式来做
def check_vowel(s1):
v=['a','e','i','o','u']
for i in v:
if s1[0].lower()==i:
return (f'{s1} start with Vowel word {i}')
else:
return (f" {s1} start with Consonants word {s1[0]}")
print(check_vowel("orange"))
my_word = "Acrobat"
the_vowel = ["a", "e", "i", "o", "u"]
if my_word[0].lower() in the_vowel:
print(my_word + " starts with a vowel")
else:
print(my_word + " doesnt start with a vowel")