python格式化輸出的三種形式


法一:

list_a = [1, 2, 3]
str_b = 'aaa'
string = "There are two contents:%s, %s" % (list_a, str_b)
print(string)
# 輸出:
# There are two contents:[1, 2, 3], aaa

法二:

list_a = [1, 2, 3]
str_b = 'aaa'
string = "There are two contents:{}, {}".format(list_a, str_b)  # {}默認為輸出對應變量的全部內容
print(string)
# 輸出:
# There are two contents:[1, 2, 3], aaa

# 若變量為列表等可以通過索引取值的數據,可以在花括號里面加入索引
string = "There are two contents:{[2]}, {}".format(list_a, str_b)
print(string)
# 輸出:
# There are two contents:3, aaa
# 可以看出,此時list_a輸出的是索引2位置的數據

法三:

list_a = [1, 2, 3]
str_b = 'ccc'
string = f"There are two contents:{list_a}, {str_b}"
print(string)
# 輸出:
# There are two contents:[1, 2, 3], ccc


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM