zoukankan      html  css  js  c++  java
  • 格式化输出_python

    一、直接使用 +
    a='chen'
    b='xiao'
    c='zan'
    print(a+b+c)

    二、利用占位符 %
    %s:占位符;%d:整数;%x:十六进制;%f:浮点数(默认6位小数)
    特别注意浮点数:
    指定长度:
             %5d     右对齐,不足左边补空格
            %-5d    - 代表左对齐,不足右边默认补空格
            %05d    右对齐,不足左边补0
          
      浮点数:
              %f       默认是输出6位有效数据, 会进行四舍五入
              %.2f    指定小数点位数的输出 ---保留小数点后2位
              '%4.8f'  4代表整个浮点数的长度,包括小数,只有当字符串的长度大于4位才起作用.不足4位空格补足,可以用%04.8使用0补足空格

    print('chenxiao%s is %d years old;pai is %.2f' %(c,15,3.141592653)) #chenxiaozan is 15 years old;pai is 3.14

    三、fromat函数
    #占位符{}
    print('chenxiaoww is {} years old;pai is {}'.format("chen",'zan'))#chenxiaoww is chen years old;pai is zan
    #占位符中填写标号
    print('chenxiaoww is {1} years old;pai is {0}'.format("chen",'zan')) #chenxiaoww is zan years old;pai is chen
    #占位符中填写名字
    print('chenxiaoww is {name} years old;pai is {age}'.format(name="chen",age='zan')) #chenxiaoww is chen years old;pai is zan

    四、f格式化输出变量
    a='123'
    print(f'chenxiaozan{a}') #chenxiaozan123
  • 相关阅读:
    ASP.NET编程的十大技巧
    C#学习心得(转)
    POJ 1177 Picture (线段树)
    POJ 3067 Japan (树状数组)
    POJ 2828 Buy Tickets (线段树)
    POJ 1195 Mobile phones (二维树状数组)
    HDU 4235 Flowers (线段树)
    POJ 2886 Who Gets the Most Candies? (线段树)
    POJ 2418 Cows (树状数组)
    HDU 4339 Query (线段树)
  • 原文地址:https://www.cnblogs.com/chenxiaozan/p/12088197.html
Copyright © 2011-2022 走看看