你怎么能把一个全名格式化成缩写呢?(X.X.X.)

2024-05-16 00:42:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着把一个全名变成一个字符串的首字母。我的逻辑是将字符串中的所有名称大写,将字符串按空格分隔成一个列表,然后选择每个索引的第一个字符,然后将这些字符合并成一个按句点分隔的字符串。在

我有点麻烦,不知道有没有更好的方法。在

以下是我目前的进展:

def main():
    fstn= input("Enter your full name:")

    fstn=fstn.title()
    fstn= fstn.split(" ")
    for i in fstn:
        fstn= i[0]
        print(fstn)


main()

这会在不同的行上打印出每个首字母,我要怎么完成?在


Tags: 方法字符串名称列表inputmaindef逻辑
2条回答
def main():
    fstn= input("Enter your full name:")
    print ('.'.join(word[0] for word in fstn.split(" ")).upper()) #for python 3


main()

嗨,看看这个例子

def main():
    fstn= input("Enter your full name:")
    fstn=fstn.title()
    fstn= fstn.split(" ")
    out_str = ""
    for i in fstn:
        out_str = out_str + i[0] + "."
    print(out_str)

main()

相关问题 更多 >