zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    01
    商城管理系统
    10
    09
    08
    07(3)
    07(2)
    07
    06
    jsp第一次作业
  • 原文地址:https://www.cnblogs.com/jaysonteng/p/12157319.html
Copyright © 2011-2022 走看看