栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 00-03 string formatting字符串格式化

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 00-03 string formatting字符串格式化

1.有三种string formatting的方式

2.第一种方法,使用formatting with placeholders 占位符格式化 %s

print("I'm going to inject %s here." %'something')

I'm going to inject something here.

也可以使用tuple来解决多个要插入的情况:

print("I'm going to inject %s text here, and %s text here." %('some','more'))

也可以:

x, y = 'some', 'more'
print("I'm going to inject %s text here, and %s text here."%(x,y))

%s 字符串内容inject进去;%r把字符串一起inject进去  t产生空格 但在%r里之后就不算特殊字符 

 

 %d是直接自取整数,不保留小数

 关于保留多少位:

(1)%7.2f

表示输出精确到小数点后2位,总长度7位(含小数点),不够时左侧补空格。

(2)%10f

输出数字总位数为10位,小数点也占一位。不够时左侧补空格。

(3)%09.f

"09."表示只输出整数位,不够9位左侧补0;同理,"010."表示只输出整数位,不够10位左侧补0。



2.第二种方法  .format()的方法

print('This is a string with an {}'.format('insert'))

This is a string with an insert 

这种方法相比于之前的%s格式输出有以下三个优点:


 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/879355.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号