Python

Python基础(1) - Print函数的用法

2020-02-17  本文已影响0人  xianling_he

使用sep参数设置字符串之间的分隔符,默认是空格

#使用逗号分隔
print("one","two",sep=",")

使用end参数设置结尾符号,默认是换行符

print("Hello")
print("World")
print("Hello",end = " ")
print("World")

可以使用%格式化字符串

s = "road"
x = len(s)
print("The length of %s is %d" % (s,x))
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=":")

总结:

1.使用sep参数设置字符串之间的分隔符,默认是空格
2.使用end参数设置结尾符号,默认是换行符
3.使用%格式化字符串
4.使用StringIO() 进行重定向,将输出放到变量里面进行输出显示

上一篇 下一篇

猜你喜欢

热点阅读