zoukankan      html  css  js  c++  java
  • 字符串的格式化

     一、百分号方式字符串的格式化

    程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式

        比如要求用户输入用户名和年龄,然后打印如下格式:

        My name is xxx,my age is xxx.

        很明显,用逗号进行字符串拼接,只能把用户输入的名字和年龄放到末尾,无法放到指定的xxx位置,而且数字也必须经过str(数字)的转换才能与字符串进行拼接。

        这就用到了占位符,如:%s、%d

        常用的格式化如下:

    tpl = "i am %s" % "alex"
     
    tpl = "i am %s age %d" % ("alex", 18)
     
    tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
     
    tpl = "percent %.2f" % 99.97623
     
    tpl = "i am %(pp).2f" % {"pp": 123.425556, }
     
    tpl = "i am %.2f %%" % {"pp": 123.425556, }

    下面我们就具体了解一下每种格式化方法的具体实现过程:

    • 打印字符串和整数
    #%s字符串占位符:可以接收字符串,也可接收数字,还可以是列表等所有数据类型,可以同时接受多个%s或%d
    print('My name is %s,my age is %s' %('lee',18))
    #%d数字占位符:只能接收数字,%.2s表示只用for循环迭代取出前两个元素
    print('My name is %.2s,my age is %d' %('lee',18))
    #接收用户输入,打印成指定格式 name=input('your name: ') age=input('your age: ') #用户输入18,会存成字符串18,无法传给%d

    输出:

    • 打印浮点数
    #默认打印小数点后六位 ,四舍五入
    tpl = "percent %f"  %99.976234544444444444
    print(tpl)
    #指定打印小数点后3位,四舍五入
    tp = "percent %.3f"  %99.976234544444444444
    print(tp)
    #指定打印小数点后3位,四舍五入,%%表示输出%
    tp = "percent %.3f %%"  %99.976234544444444444
    print(tp)

    输出:

    • 使用字典进行格式化输出
    #括号里是字典的key,打印时打印的是key对应的value,括号后跟的是value的数据类型
    tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
    print(tpl)

    输出:

    • 输出加颜色或阴影
    tpl = "i am 33[35;1m%(name)s33[0m  age %(age)d" % {"name": "alex", "age": 18}      ##改变35这里的数值选择颜色、格式(阴影还是字体颜色或加粗等)
    print(tpl)

    输出:

    • 指定分隔符输出
    print('root','X','123','134')
    print('root','X','123','134',sep=":")    ##linux用户信息的标准格式,也可以使用“root” + “X” + "123"这种方式,但是效率太低

    输出:

     二、format方式字符串的格式化

       常用的格式化如下:

    tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
      
    tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])   ##传的是可迭代对象,这种情况必须一一对应
      
    tpl = "i am {0}, age {1}, really {0}".format("seven", 18)    ##可以不一一对应,format的参数是一个元组,前边的中括号中填参数的索引,参数的格式大于等于取的范围
      
    tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])  ##列表
      
    tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)    ##字典
      
    tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})   ##字典
      
    tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])  ##字典的迭代
      
    tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)   ##指定该位置的数据类型
      
    tpl = "i am {:s}, age {:d}".format(*["seven", 18])
      
    tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
      
    tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
     
    tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)    ##b,o,d分别表示二、八、十进制,x表示十六进制的小写,X表示大写,{:%}表示将该数值转化为百分制默认保留小数点后六位
    输出:numbers: 1111,17,15,f,F, 1587.623000%
    tpl
    = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) ##0表示取得第一个 tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
  • 相关阅读:
    c# 获取网络流量
    【转】C#中如何实现左截取和右截取字符串
    dropdownlist同时绑定数据库和自定义内容
    菜鸟级别的WCF入门学习
    gridview里日期显示格式
    ValidationSummary控件不弹出错误提示框
    WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping。
    datagridview控件去除页码
    时间大小比较
    时间为23:59:59
  • 原文地址:https://www.cnblogs.com/foever-lee/p/10309898.html
Copyright © 2011-2022 走看看