python之输出语句

输出语句

多级打印

  • 代码

    print("1","+","2","=",1+2)
    
  • 结果

    1 + 2 = 3
    

结束符

print打印出的元素默认会用\n作为结束符#如果想改变结束符的话,用end关键字来改变

  • 代码

    list = ['1','2','3'];
    #输出元素之间用分隔符隔开
    for elem in list:
        print(elem)
    for elem in list:
        print(elem)
    #不换行输出
    print("\n")
    for elem in list:
        print(elem,end="")
    print("\n")
    #不换行输出
    for elem in list:
        print(elem,end="-")
    print("\n")
    #不换行输出
    for elem in list:
        print(elem,end=",")
    print("\n")
    
  • 结果

    1
    2
    3
    1
    2
    3
    
    
    123
    
    1-2-3-
    
    123

分隔符

  • 代码

    #分隔符,用于一条print语句一次打印多个元素,默认是空格
    print("qw","wa","we")
    #相邻之间用“,”分隔
    print("qw","wa","we",sep=",")
    #相邻元素用“-”分隔
    print("qw","wa","we",sep="-")
    
  • 结果

    qw wa we
    qw,wa,we
    qw-wa-we
    

格式化输出

  • 代码

    #字符串
    print ("His name is %s"%("Aviad"))
    #打印整数
    print ("He is %d years old"%(25))
    #打印浮点数
    print ("His height is %f m"%(1.83))
    #打印浮点数保留位数
    print ("His height is %.2f m"%(1.83))
    
  • 结果

    His name is Aviad
    He is 25 years old
    His height is 1.830000 m
    His height is 1.83 m
    

你可能感兴趣的:(python)