python格式化输出的三种形式
生活随笔
收集整理的這篇文章主要介紹了
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方法二:
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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總結
以上是生活随笔為你收集整理的python格式化输出的三种形式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python编码类型互转总结
- 下一篇: 用python实现图书管理系统