Write函数接受两个单词的字符串,如果两个单词在python中以相同的字母开头,则返回True
我想写一个函数,它接受两个单词的字符串,如果两个单词在python中以相同的字母开头,则返回True 样本输入: 动物饼干(‘平头骆驼’)--> 动物饼干(“疯狂袋鼠”)->错误Write函数接受两个单词的字符串,如果两个单词在python中以相同的字母开头,则返回True,python,Python,我想写一个函数,它接受两个单词的字符串,如果两个单词在python中以相同的字母开头,则返回True 样本输入: 动物饼干(‘平头骆驼’)--> 动物饼干(“疯狂袋鼠”)->错误 上面的函数应该可以做到这一点。不确定它是否是您要查找的,但是您可以通过获取字符串的索引来获取字符串的每个字母,就像在数组中一样。所以应该是这样的: def check_letter(string): # Split function splits string making an array(split
上面的函数应该可以做到这一点。不确定它是否是您要查找的,但是您可以通过获取字符串的索引来获取字符串的每个字母,就像在数组中一样。所以应该是这样的:
def check_letter(string):
# Split function splits string making an array(splits at space at this example).
words = string.split()
# [0][0] means first element and first symbol, [1][0] - 2nd elem, 1st symbol
if words[0][0] == words[1][0]:
return True
else:
return False
下面的代码首先将字符串的第一个字符存储在变量中,然后定位字符串中的第一个空格,然后比较第一个字符和空格后的第一个字符,并相应地返回
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]
你做了什么工作来实现这一点吗?嗨,泰勒,是的,但我不能得出任何结论,我正在使用string.split(“”)方法来实现同样的目标。但无法思考代码的其余部分。因此,请改为提问。谢谢。。梅里格。。添加了另外一种方法,我尝试了双重索引。def animal_crackers(text):list_String=text.split()返回list_String[0][0]==list_String[1][0]谢谢,但这是一个冗长的方法。。
def check_letter(string):
# Split function splits string making an array(splits at space at this example).
words = string.split()
# [0][0] means first element and first symbol, [1][0] - 2nd elem, 1st symbol
if words[0][0] == words[1][0]:
return True
else:
return False
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]