10 - python print函数

1. 使用print 函数输出字符串时,如何用逗号 (,) 分隔

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

print('aa', 'bb')
# sep 可以用一个字符串作为分隔符
print('aa', 'bb', sep=',')
aa bb
aa,bb

2. 使用print 函数输出字符串时,如何不换行

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

print('hello')
print('world')

print('hello', end=' ')
print('world')
hello
world
hello world

3. 如何用print 函数格式化输出

# 可以使用 % 格式化字符串

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=': ')
The length of road is 4
result_str: The length of road is 4

11 - 去掉列表或元组中的重复元素

发布了107 篇原创文章 · 获赞 101 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_29339467/article/details/104181079