python开发之str.format()用法实例分析
作者:Hongten 时间:2022-01-15 22:10:23
本文实例分析了python开发之str.format()用法。分享给大家供大家参考,具体如下:
格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过
下面看看python中的字符串格式函数str.format():
#使用str.format()函数
#使用'{}'占位符
print('I\'m {},{}'.format('Hongten','Welcome to my space!'))
print('#' * 40)
#也可以使用'{0}','{1}'形式的占位符
print('{0},I\'m {1},my E-mail is {2}'.format('Hello','Hongten','hongtenzone@foxmail.com'))
#可以改变占位符的位置
print('{1},I\'m {0},my E-mail is {2}'.format('Hongten','Hello','hongtenzone@foxmail.com'))
print('#' * 40)
#使用'{name}'形式的占位符
print('Hi,{name},{message}'.format(name = 'Tom',message = 'How old are you?'))
print('#' * 40)
#混合使用'{0}','{name}'形式
print('{0},I\'m {1},{message}'.format('Hello','Hongten',message = 'This is a test message!'))
print('#' * 40)
#下面进行格式控制
import math
print('The value of PI is approximately {}.'.format(math.pi))
print('The value of PI is approximately {!r}.'.format(math.pi))
print('The value of PI is approximately {0:.3f}.'.format(math.pi))
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
for name, phone in table.items():
print('{0:10} ==> {1:10d}'.format(name, phone))
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ''Dcab: {0[Dcab]:d}'.format(table))
运行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
I'm Hongten,Welcome to my space!
########################################
Hello,I'm Hongten,my E-mail is hongtenzone@foxmail.com
Hello,I'm Hongten,my E-mail is hongtenzone@foxmail.com
########################################
Hi,Tom,How old are you?
########################################
Hello,I'm Hongten,This is a test message!
########################################
The value of PI is approximately 3.141592653589793.
The value of PI is approximately 3.141592653589793.
The value of PI is approximately 3.142.
Jack ==> 4098
Sjoerd ==> 4127
Dcab ==> 7678
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
>>>
希望本文所述对大家Python程序设计有所帮助。
标签:python,format
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解python中GPU版本的opencv常用方法介绍
2023-09-19 22:55:17
![](https://img.aspxhome.com/file/2023/5/82555_0s.jpg)
Django结合ajax进行页面实时更新的例子
2023-02-14 21:15:45
![](https://img.aspxhome.com/file/2023/1/80581_0s.jpg)
解决jupyter 在浏览器中 代码不执行的问题
2023-05-08 01:21:35
![](https://img.aspxhome.com/file/2023/9/85059_0s.jpg)
互联网产品设计零碎记
2010-09-25 12:49:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/diedai-600x64-87s.png)
PHP的PDO预处理语句与存储过程
2023-06-17 11:49:59
使用python爬取taptap网站游戏截图的步骤
2021-09-17 07:44:34
![](https://img.aspxhome.com/file/2023/6/68316_0s.png)
Python 函数返回值的示例代码
2021-10-12 19:30:23
Web标准在中国
2008-11-26 11:27:00
SQL建立数据库及删除数据库命令
2011-12-01 10:23:21
FSO无效的过程调用或参数问题
2010-03-25 21:49:00
详解bootstrap导航栏.nav与.navbar区别
2023-08-15 19:18:42
![](https://img.aspxhome.com/file/2023/7/56077_0s.png)
SQL重复记录处理(查找,过滤,删除)
2008-11-17 20:47:00
利用PyInstaller将python程序.py转为.exe的方法详解
2021-07-09 16:41:51
将SQL 2000日志迁移到SQL Server 2008
2009-03-25 16:20:00
pycharm 2019 最新激活方式(pycharm破解、激活)
2021-09-15 13:07:01
![](https://img.aspxhome.com/file/2023/9/89219_0s.jpg)
Python中如何实现MOOC扫码登录
2021-10-08 14:24:50
![](https://img.aspxhome.com/file/2023/1/66861_0s.png)
一个较复杂的字符串截取函数
2009-11-02 10:45:00
Python基于Logistic回归建模计算某银行在降低贷款拖欠率的数据示例
2023-11-27 21:26:53
![](https://img.aspxhome.com/file/2023/8/70208_0s.png)
python中pass语句用法实例分析
2021-08-12 01:49:13
Python pandas之多级索引取值详解
2022-05-09 14:12:31
![](https://img.aspxhome.com/file/2023/4/87524_0s.png)