网络编程
位置:首页>> 网络编程>> Python编程>> python中.format()方法使用详解

python中.format()方法使用详解

作者:IT之一小佬  发布时间:2021-07-19 03:38:07 

标签:python,.format,使用

前言

format语法格式:

  • str.format()

  • str是指字符串实例对象,常用格式为‘ ’.format()

def format(self, *args, **kwargs): # known special case of str.format
       """
       S.format(*args, **kwargs) -> str

Return a formatted version of S, using substitutions from args and kwargs.
       The substitutions are identified by braces ('{' and '}').
       """
       pass

format参数格式:

'{[index][ : [fill] align][sign][#][width][.precision][type]} {……}{……} '.format()
注意,格式中的[ ]内的参数都是可选参数,可以使用也可以不使用

  • index:指定冒号**:**后面出现的参数在‘format()’中的索引值,如果没有,则以format()中的默认顺序自动分配

  • fill:指定空白处的填充符。align:指定数字的对齐方式:align含义<right-aligned 左对齐(对于大部分对象时为默认)>right-aligned 右对齐 (对于数字时为默认)=数据右对齐,同时将符号放置在填充内容的最左侧,该选项只对数字类型有效^数据居中,此选项需和 width 参数一起使用

  • sign:指定有无符号数,此参数的值以及对应的含义如表所示sign 参数含义+正数前面添加 &lsquo; + &rsquo; ,负数前面加 &lsquo; - &rsquo;-正数前面不添加 &lsquo; + &rsquo; ,负数前面加 &lsquo; - &rsquo;space正数前面添加 &lsquo; 空格 &rsquo; ,负数前面加 &lsquo; - &rsquo;#对于二进制数、八进制数和十六进制数,使用此参数,各进制数前会分别显示 0b、0o、0x前缀;反之则不显示前缀width:指定输出数据时所占的宽度. precision:如果后面存在type参数,则指的是保留小数的位数,如果type参数不存在,则是指有效数字的位数type:指定输出数据的具体类型

python中.format()方法使用详解

一、简单使用方法

1.无参数

foramt会把参数按位置顺序来填充到字符串中,第一个参数是0,然后1 &hellip;&hellip;也可以不输入数字,则会按照顺序自动分配,而且一个参数可以多次插入

示例代码:

name = '张三'
age = 25
sex = '男'

print('{}、{}、{}'.format(name, age, sex))  #  占位符不指定顺序
print('{0}、{1}、{2}'.format(name, age, sex)) #  占位符制定顺序
print('{0}、{2}、{1}'.format(name, age, sex)) #  换一下顺序试试
print('{0}、{2}、{1}、{0}、{2}、{1}'.format(name, age, sex))

运行结果:

python中.format()方法使用详解

2. key value

示例代码:

name1 = '张三'
age1 = 25
sex1 = '男'

print('name:{name}、age={age}、sex:{sex}'.format(name=name1, age=age1, sex=sex1))
print('name:{name}、sex:{sex}、age={age}'.format(name=name1, age=age1, sex=sex1))

运行结果:

python中.format()方法使用详解

3. 列表

示例代码:

lst1 = ['张三', '男', 25]
lst2 = ['李四', '男', 28]

print('name:{Lst[0]},sex:{Lst[1]},age:{Lst[2]}'.format(Lst=lst1))
print('name:{0[0]},sex:{0[1]},age:{0[2]}'.format(lst1))
print('name:{0[0]},sex:{0[1]},age:{0[2]}'.format(lst2))
print('name:{0[0]},sex:{0[1]},age:{0[2]}'.format(lst1, lst2))
print('name:{1[0]},sex:{1[1]},age:{1[2]}'.format(lst1, lst2))
print('name:{0[0]},sex:{0[1]},age:{0[2]},name:{1[0]},sex:{1[1]},age:{1[2]}'.format(lst1, lst2))

运行结果:

python中.format()方法使用详解

4. 字典

示例代码:

dic1 = {'name': '张三', 'sex': '男', 'age': 25}
dic2 = {'name': '李四', 'sex': '男', 'age': 28}

print('name:{Dic[name]},sex:{Dic[sex]},age:{Dic[age]}'.format(Dic=dic1))
print('name:{name},sex:{sex},age:{age}'.format(**dic2))

运行结果:

python中.format()方法使用详解

5. 类

示例代码:

class Info(object):
   name = '张三'
   sex = '男'
   age = 25
print('name:{info.name},sex:{info.sex},age:{info.age}'.format(info=Info))

运行结果:

python中.format()方法使用详解

6. 魔法参数

*args表示任何多个无名参数,它是一个tuple or list;**kwargs表示关键字参数,它是一个 dict。

 示例代码:

lst = [',', '.']
dic = {'name': '张三', 'sex': '男', 'age': 25}
print('name:{name}{0}sex:{sex}{0}age:{age}{1}'.format(*lst, **dic))

运行结果:

python中.format()方法使用详解

二、参数使用方法

示例代码1:

#  python :^:代表居中显示,数字567,位数width=10,fill=*(填充符为*)
print('{:*^10}'.format(567))

运行结果:

python中.format()方法使用详解

 示例代码2:

# python :0是填充符,2是width,表示位数为2
print('{:02}:{:02}:{:02}'.format(13, 4, 57))
print('{:05}:{:05}:{:05}'.format(13, 4, 57))

运行结果:

python中.format()方法使用详解

来源:https://blog.csdn.net/weixin_44799217/article/details/125829674

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com