简体   繁体   English

如何提取某些单词的第一个字母

[英]How to extract the first letter of certain words

I have an assignment that wants me to 'nickname' an input string.我有一项作业要我为输入字符串命名“昵称”。 So far I was able to extract the first letter of every word in the string, but I need to exempt pronouns and words smaller than three characters.到目前为止,我能够提取字符串中每个单词的第一个字母,但我需要排除代词和小于三个字符的单词。

This is what I have so far:这是我到目前为止:

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())

try尝试

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM