【问题标题】:How do I return my output instead of print?如何返回我的输出而不是打印?
【发布时间】:2025-12-24 14:45:06
【问题描述】:
def spin_words(sentence):
    adjusted_string = sentence.split()
    for i in adjusted_string:
        if len(i) > 5:
            print(i[::-1], end = ' ')
        else:
            print(i, end = ' ')

问题是要求获取一个字符串并返回相同的字符串,但是所有五个或更多字母的单词都颠倒了

【问题讨论】:

  • 你试过用return关键字代替print吗?

标签: python string return


【解决方案1】:
def spin_words(sentence):
    splitted_string = sentence.split()
    reversed_fives = [s[::-1] if len(s) >= 5 else s for s in splitted_string]
    return " ".join(reversed_fives)

【讨论】:

    最近更新 更多