打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python 字符串format方法格式化输出字符串

你可以用字符串的format方法来格式化输出字符串。 比如;

>>> print 'We are the {0} who say "{1}!"'.format('knights', 'Ni')
We are the knights who say "Ni!"


括号内的字符(称为格式字段)被替换的对象。{}括号中的数字是指替换的位置,里面的数字,比如0,1表示替换元组的索引位置。

>>> print '{0} and {1}'.format('spam', 'eggs')
spam and eggs
>>> print '{1} and {0}'.format('spam', 'eggs')
eggs and spam


如果使用关键字参数的格式方法,其值被称为使用的参数名称。

>>> print 'This {food} is {adjective}.'.format(
... food='spam', adjective='absolutely horrible')
This spam is absolutely horrible.


下面是位置和关键字参数的任意组合:

>>> print 'The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
... other='Georg')
The story of Bill, Manfred, and Georg.

 


延伸阅读:
·python dir()函数返回一个字符串列表
·python 字符串特点
·python 字符串split的用法(string split)
·python print 连接字符串
·python list 合并连接字符串
·python int 字符串 string to int
·python获取字符串的第一个字符和最后一个字符
·python字符串格式化输出总结
·python 字符串去除空格用strip()方法
·python 字符串转换大写upper方法
·python 字符串转换小写lower方法
·python字符串join方法操作
·python字符串查找(find)方法
·python string字符串count方法
·python 字符串首字母大写
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python自学知识点整理
学Python还只会用%格式化输出?赶紧点赞收藏吧!
【Python】入门了解
Python 列表(Lists)
Python 列表(List)操作方法详解
python菜鸟教程学习12:输入输出
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服