python字符串的格式化输出

格式化字符串是程序设计语言中用于指定输出参数的格式化与相对位置的字符串参数。其中的转换说明用于把随后的对应一个或多个函数参数转换为相应的格式输出:格式化字符串中转换说明以外的其他字符原样输出。

1
>>>"I like %s"

  在这个字符串中,有一个字符 %s 就是一个占位符,这个占位符可以被其他字符串取代

1
2
>>>"I like %s" % "girl"
'I like girl'

  自Python2.6以后提倡使用字符串的 format() 方法:string.format(*args,**kwargs)

1
2
>>>"I like {1} and {0}".format('girl','dog')
'I like dog and girl'

  

1
2
>>>"I like {0} and {1}".format('girl','dog')
'I like girl and dog'

  {0}和{1}作为占位符占据两个位置,然后调用str.format()方法分别把“girl”和“dog”两个参数传入对应的占位符。str.format()方法返回的是一个字符串(“I like girl and dog”)

既然是格式化,那么就一定会有各种方便适应的格式,让输出的结果符合指定格式

1
2
>>>"I like {0:5} and {1:>5}".format('girl','dog')
'I like girl  and   dog'

  {0:5}表示第一个位置占用五个字符默认左对齐,{1:>5}表示第二个位置占用五个字符表示右对齐

1
2
>>>"I like {0:^5} and {1:^5}".format('girl','dog')
'I like girl  and  dog '

  两个占位符都占用五个字符,并且参数在五个占位符中居中对其

1
2
>>>"I like {0:^5.2} and {1:^5.2}".format('girl','dog')
'I like  gi   and  do  '

  两个占位符都占用五个字符,并且参数在五个占位符中居中对其,传入的字符串只截取两个字符。girl->gi  dog->do

str.format() 中除了可以传入字符串,还可以传入数字

1
"I like {0:10d} and {1:10.1f}".format(520,5.20)<br>'I like        520 and        5.2'

  传入数字默认右对齐,{1:10.1f}中  .1  表示保留小数点后一位,d代表整数,f代表浮点数,如果不在传入浮点数的时候不用 f 则会出现下面情况

1
2
>>>"I like {0:10} and {1:10.1}".format(520,5.20)
'I like        520 and      5e+00'

  

1
2
>>>"I like {sex} and {pet}".format(pet = 'dog',sex = 'girl')
'I like girl and dog'

 这是一种关于字典的格式化方法

1
2
3
>>>data = {'name':'jiaxiangfei','sex':'boy'}
>>>'{name} is a {sex}'.format(**data)
'jiaxiangfei is a boy'

  str.format(*args,**kwargs) 只是字符串的格式化方法

转载于:https://www.cnblogs.com/hanzeng1993/p/11236003.html

python 字符串format使用相关推荐

  1. python字符串format格式化三

    python字符串format左中右对齐 print("第{ya:02.0f}章,寻找超越的方式".format(ya = 1)) .0f的意思是小数部分忽略 ya:02的功能是格 ...

  2. python字符串format方法参数解释,一文秒懂!Python字符串格式化之format方法详解

    一文秒懂!Python字符串格式化之format方法详解 一文秒懂!Python字符串格式化之format方法详解 format是字符串内嵌的一个方法,用于格式化字符串.以大括号{}来标明被替换的字符 ...

  3. python字符串format和center居中应用(三分钟读懂)

    字符串format和center的关系 使用center方法使格式为居中 位宽为20 填充空格 print("<" + "居中".center(20) + ...

  4. python 字符串format格式化一

    字符串的format方法(format格式化) 字符串格式化参数使用一对花括号{} 支持按顺序指定格式化参数值和关键字格式化 print("{} {} {}".format(1,2 ...

  5. python花式输出_关于python字符串format的一些花式用法

    目录: 前提: python中字符串的format功能非常强大,可以说完全能够替代其他的字符串使用方法,但是在后期的项目开发中使用的并不是特别的多, 以至于想用的时候会想不起来准确的使用方法,所以在这 ...

  6. python字符串format格式化

    print("{}{}{}".format(1,2,3),"按顺序填入数字1") print("{}{}{}".format(3,2,1), ...

  7. python字符串format格式化二

    常用的字符串格式化类型符 print("{abc!a}".format(abc="我")) # 将字符串按Unicode编码输出 print("{ab ...

  8. Python字符串格式()

    Python String format() function is used to create a formatted string from the template string and th ...

  9. Python字符串串联

    String Concatenation is a very common operation in programming. Python String Concatenation can be d ...

最新文章

  1. Pri3D:一种利用RGB-D数据固有属性完成3D场景感知的表示学习方法
  2. 下一代防火墙市场高速增长,年复合增长率为13.6%
  3. Discrete Log Algorithms :Baby-step giant-step
  4. 如何打造应对超大流量的高性能负载均衡?
  5. EMC -- Windows下卸载 Content Server
  6. mybatis问题。foreach循环遍历数组报错情况,及其解决方法
  7. 3. Ubuntu LAMP 环境搭建
  8. UIFont 字体设置
  9. itpt_TCPL 第一章:C简要教程
  10. js在字符串中间插入横线
  11. 本地idea连接Linux上Redis出现RedisConnectionException: Unable to connect to xxx.xxx.xxx.xxx:6379的问题
  12. centos 调教日记
  13. 智能马桶雷达应用方案,智能雷达感应控制,雷达感应模组技术应用
  14. SpringBoot-starter-data整合Elasticsearch
  15. SEC合规审查办公室2018工作重点:加强对加密货币企业信息披露的监管
  16. vue 渲染的list 数据交换顺序,简单就可以实现动画效果
  17. spring框架学习(一):Bean的装配方式 ——基于注解的装配、自动装配
  18. 调整系统时间,导致https站点无法访问,skype无法登陆等问题
  19. 《我是谁》——孙溟㠭(展)书法篆刻
  20. 资料链接 网络/系统/华为

热门文章

  1. enter power save mode解决
  2. 如何使用 SSH 控制连接 Windows 服务器
  3. java 复制对象_Java程序员必备:序列化全方位解析
  4. python定义一个列表_如何在Python中创建用户定义的列表?
  5. [译]SQL SERVER 2016 – Temporal Tables
  6. eclipse/myeclipse中快捷键 Ctrl+shift+down/up 出现屏幕颠倒的解决方法
  7. 解决Yii2邮件发送问题(结果返回成功,但接收不到邮件)
  8. Studio 一些使用
  9. Perl学习笔记(六)--文件(一)
  10. CentOS 更改MySQL数据库目录位置