gpt4 book ai didi

python - 如何将全名格式化为首字母缩写? (X.X.X.)

转载 作者:行者123 更新时间:2023-12-01 02:31:51 25 4
gpt4 key购买 nike

我正在尝试将全名转换为一个字符串的首字母缩写。我的逻辑是将字符串中的所有名称大写,将字符串按空格分成一个列表,然后选择每个索引的第一个字符,然后将字符连接成一个用句点分隔的字符串。

我在这方面遇到了麻烦,不确定是否有更好的方法。

这是我当前的进度:

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

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


main()

这会在不同的行上打印出每个首字母,我将如何完成这个?

最佳答案

def main():
fstn= input("Enter your full name:")
print ('.'.join(word[0] for word in fstn.split(" ")).upper()) #for python 3


main()

关于python - 如何将全名格式化为首字母缩写? (X.X.X.),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46733638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com