• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python开发之str.format()用法实例分析

python开发之str.format()用法实例分析

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python str.format,str.format,str.format函数,str.format d i,python str等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例分析了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))

</div>

运行效果:

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
>>> 

</div>

希望本文所述对大家Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python中str.format()详解
  • Python中字符串格式化str.format的详细介绍
  • python开发之str.format()用法实例分析

相关文章

  • Python2.7简单连接与操作MySQL的方法
  • python的id()函数解密过程
  • Python设计模式之单例模式实例
  • Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法
  • 跟老齐学Python之数据类型总结
  • Python获取运行目录与当前脚本目录的方法
  • python机器学习库xgboost的使用
  • python判断一个集合是否包含了另外一个集合中所有项的方法
  • Python中os和shutil模块实用方法集锦
  • python连接mysql调用存储过程示例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python实现爬虫下载美女图片
    • python self,cls,decorator的理解
    • Python异常学习笔记
    • python任务调度实例分析
    • 在Python的Django框架下使用django-tagging的教程
    • python使用分治法实现求解最大值的方法
    • Python Django使用forms来实现评论功能
    • Python选课系统开发程序
    • python数据结构之图深度优先和广度优先实例详解
    • python的Template使用指南

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有