首页 > 解决方案 > Python如何将打印语句存储在变量中?

问题描述

我喜欢将两个打印语句保存在 2 个不同的变量中。我怎样才能做到这一点?

 with open(file_to_open) as f:

    for line in f:
        # split the line
        line = line.strip()
        columns = line.split(",")

        if columns[0] == "1":
           print(line, end='')
        if columns[0] == "2":
            print(line, end='')

标签: pythonprinting

解决方案


print(x)函数隐含

  1. 来电str(x)
  2. 显示它
  3. 返回None

所以,你不能做

 stored = print(x)

相反,写

stored_value = str(x)

推荐阅读