如果输入是:简傻的无名氏输出是: Doe,J.S。
如果输入是: Julia Clark输出是: Clark,J.问题:
我可以让它要么跑,要么两者兼而有之。
这只对输入简傻无名氏有效,但对输入朱莉娅克拉克不起作用。我可以让它同时运行,但不能同时运行。
string = input()
words = string.split()
title = ''
for word in words:
title += word
another = (words[-1]+',')
second = (words[0])
third = (words[1])
fourth = second + third
upper = ''
for char in fourth:
if char.isupper():
upper += char
join_string = '.'.join(upper)
print(another, join_string + '.')发布于 2022-07-16 16:54:14
您可以像words[1]一样使用固定索引,使用切片words[:-1]来获取除最后一个值以外的所有值。
def shorten(string):
words = string.split()
result = words[-1] + ', '
for char in ''.join(words[:-1]):
if char.isupper():
result += char + "."
print(result)shorten("Julia Clark") # Clark, J.
shorten("Jane Silly Doe") # Doe, J.S.
shorten("Jane Silly Jack James Doe") # Doe, J.S.J.J.https://stackoverflow.com/questions/73006013
复制相似问题