Write a函数接受两个单词的字符串,如果两个单词在python中都以同一个字母开头,则返回True

2024-09-28 21:27:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个函数,它接受一个两个单词的字符串,如果两个单词在python中都以同一个字母开头,则返回True。在

样本输入: 动物饼干(平头驼)-->;真的 动物饼干(疯狂袋鼠)-->;错误


Tags: 函数字符串gttrue错误字母单词袋鼠
3条回答
def animal_crackers(string):
    s1, s2 = string.split(' ')
    return s1[0].upper() == s2[0].upper()

下面的代码首先将字符串的第一个字符存储在变量中,然后定位字符串中的第一个空格,然后比较第一个字符和空格后的第一个字符并相应地返回。在

def letter_check(s):

     first_letter= s[0]  #stores the first character of the string in a variable

     for i in range (0,len(s)):
         if(s[i]==" "):         #locates the space in the string
             space_id=i
     if(s[0]==s[space_id+1]):   #compares the first letter with the letter after space
         return[True]
     else:
         return[False]
def animal_crackers(words):
    first_word = words.split(" ")[0].lower()
    second_word = words.split(" ")[1].lower()
    if first_word[0] == second_word[0]:
        return True
    else:
        return False

上面的函数应该能做到这一点。在

相关问题 更多 >