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 - 去掉列表或元組中的重複元素

發佈了128 篇原創文章 · 獲贊 128 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章