Python:将打印输出分配给一个变量 [英] Python: Assign print output to a variable

查看:549
本文介绍了Python:将打印输出分配给一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将 print 函数(或任何函数)的输出分配给一个变量。举个例子:

I would like to know how to assign the output of the print function (or any function) to a variable. To give an example:

import eyeD3
tag = eyeD3.Tag()
tag.link("/some/file.mp3")
print tag.getArtist()

如何做我将 print print.getArtist 的输出分配给一个变量?

How do I assign the output of print tag.getArtist to a variable?

推荐答案

Python中的 print 语句将其参数转换为字符串,并将这些字符串输出到stdout。要将字符串保存到变量中,只需将其转换为字符串:

The print statement in Python converts its arguments to strings, and outputs those strings to stdout. To save the string to a variable instead, only convert it to a string:

a = str(tag.getArtist())

这篇关于Python:将打印输出分配给一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆