微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何提取某些单词的首字母

如何解决如何提取某些单词的首字母

我有一个作业,希望我“昵称”输入字符串。到目前为止,我已经能够提取字符串中每个单词的第一个字母,但是我需要排除代词和少于三个字符的单词。

这是我到目前为止所拥有的:

def nickname():
    name = input('Would you like to nickname a sentence or would you like to quit?: ')
    if name == "quit":
        print("bye")
    words = name.split()
    letters = [word[0] for word in words]
    return(" ".join(letters).upper())

解决方法

尝试

def nickname():
    name = input('Would you like to nickname a sentence or would you like to quit?: ')
    if name == "quit":
        print("bye")
    words = name.split()
    letters = [word for word in words if len(word)>3]
    return(" ".join(letters).upper())
,
def nickname():
    name = input('Would you like to nickname a sentence or would you like to quit?:   ') 
    if name == "quit":
        print("bye")
    else:
      words = name.split()
      letters = [word[0] for word in words if len(word)>3]
      return("".join(letters).upper())

print(nickname())

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。