Skip to content

如何使用print函数格式化输出 #24

@Sogrey

Description

@Sogrey
s = 'road'
x = len(s)
print('The length of %s is %d' % (s,x))   ## The length of road is 4

from io import StringIO
import sys
old_stdout = sys.stdout
result = StringIO()
sys.stdout = result
print('The length of %s is %d' % (s,x))
sys.stdout = old_stdout
result_str = result.getvalue()
print('result_str',result_str,sep=':')   ## result_str:The length of road is 4

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions