<p>在尝试找出输出“短语”首字母的函数时遇到一些困难。这就是我目前所拥有的。谢谢你的帮助</p>
<pre><code>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
</code></pre>
<p>字符串只是一个字符数组,在Python中,您只需访问字符串中的第一个位置AKA[0]:</p>
<pre><code>def initials(phrase):
words = phase.split()
result = ""
for word in words:
result += word[0]
return result
</code></pre>
<p>如果您想要全部大写,只需在“word[0]”中使用.upper()</p>