在尝试找出输出“短语”首字母的函数时遇到一些困难。这就是我目前所拥有的。谢谢你的帮助
def initials(phrase):
words = phase.split
result = ""
for word in words:
result += 1
return result
print(initials("United Nations")) # Should be: UN
print(initials("United States of America")) # Should be: USOA
print(initials("Banana Boat")). # Should be: BB
尽量遵循采购订单的逻辑,不要对结构进行额外更改。只需修复错误:
可能有更多的python方法可以达到同样的效果,但这里试着回答PO的问题
或者只使用一个衬套,使用如下发电机:
字符串只是一个字符数组,在Python中,您只需访问字符串中的第一个位置AKA[0]:
如果您想要全部大写,只需在“word[0]”中使用.upper()
嗯,a
word
的首字母是word[0]
,但是你想大写,所以就去吧相关问题 更多 >
编程相关推荐