在使用print格式化輸出時(shí),可以通過(guò)元組和集合的操作來(lái)實(shí)現(xiàn)更靈活的輸出效果。以下是一些元組和集合操作技巧:
t = (1, 2, 3)
print("Tuple elements are: {}, {}, {}".format(*t))
s1 = {1, 2, 3}
s2 = {2, 3, 4}
print("Elements in set1 but not in set2 are: {}".format(s1 - s2))
s1 = {1, 2, 3}
s2 = {2, 3, 4}
print("Common elements in set1 and set2 are: {}".format(s1 & s2))
s1 = {1, 2, 3}
s2 = {2, 3, 4}
print("All elements in set1 and set2 are: {}".format(s1 | s2))
這些操作技巧可以幫助我們更好地控制輸出格式,使輸出結(jié)果更加靈活和易讀。