【问题标题】:How to print a returned tuple in another method [closed]如何以另一种方法打印返回的元组[关闭]
【发布时间】:2013-12-25 11:50:19
【问题描述】:

如果我有两种方法:

  1. 一种是将string 更改为tuple 并返回它的方法。
  2. 其他方法打印出返回的tuple

最后我该怎么做?

输入类似于"12345 firstName lastName otherInfo"

def information(sentence):
    splitUp = sentence.split(' ')
    return sentence

def printAsString():
    "".join(sentence)
    print(sentence)

【问题讨论】:

  • 您能提供输入和预期输出吗?
  • 您是否尝试将参数传递给printAsString?你的 information 函数已经接受了一个参数,所以你知道它是如何工作的。

标签: python return tuples


【解决方案1】:

这是一种方法。但你的目标是什么?这段代码有点无意义...

def information(sentence):
    '''Splits the sentence on space and returns it as tuple'''
    split_up = tuple(sentence.split(' '))
    return split_up

def print_as_string():
    '''Prints tuple as string'''
    sentence = "welcome to SO dear friend"
    print(" ".join(information(sentence)))

if __name__ == "__main__":
    print_as_string()

【讨论】:

【解决方案2】:

应该是

print ''.join(employeeInfo)

但我的问题是,employeeInfo 到底是从哪里形成的?

【讨论】:

    猜你喜欢
    • 2016-02-11
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2011-08-01
    • 2012-12-06
    相关资源
    最近更新 更多