[Python] format格式化函数
Python 函数 格式化 format
2023-09-11 14:22:54 时间
格式化字符串函数 str.format()
format函数可以接受无限个参数,位置可以不按顺序
'{} {}'.format('Hello','World') # 不设置指定位置,按默认顺序
# 'Hello World'
'{0} {1}'.format('Hello','World') # 设置指定位置
# 'Hello World'
'{1} {0} {1}'.format('Hello','World') # 设置指定位置
# 'World Hello World'
也可以设置参数:
# 网站名:CSDN, 地址 https://www.csdn.net
print("网站名:{name}, 地址 {url}".format(name="CSDN", url="https://www.csdn.net"))
# 通过字典设置参数
site = {"name": "CSDN", "url": "https://www.csdn.net"}
print("网站名:{name}, 地址 {url}".format(**site))
# 通过列表索引设置参数
my_list = ['CSDN', 'https://www.csdn.net']
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list)) # "0" 是必须的
可以使用大括号{}来转义大括号,如下实例:
print ("{} 对应的位置是 {{0}}".format("runoob")) # runoob 对应的位置是 {0}
相关文章
- python应用-综合应用题解决
- Python endswith() 函数
- Python 日期和时间_python 当前日期时间_python日期格式化
- 使用Python循环插入10万数据
- Python - 属性简介"__name__"
- Python 日期和时间_python 当前日期时间_python日期格式化
- Python 日期和时间_python 当前日期时间_python日期格式化
- Python+selenium自动化测试元素操作
- python的内置函数
- python: 安装DeOldify库:黑白图片上色(Python 3.7.15)
- Python函数详解(四)——Python函数参数使用注意事项
- [LINK]Python服务器开发一:python基础
- Python之多线程:python多线程设计之同时执行多个函数命令详细攻略
- Python语言学习之文件夹那些事:python和文件夹的使用方法之详细攻略
- Python:python代码编程带你玩转双色球(了解双色球的概率逻辑)
- Python之多线程:python多线程设计之同时执行多个函数命令详细攻略
- Python语言学习之双下划线那些事:python和双下划线使用方法之详细攻略
- IDE之VS:利用 Visual Studio中的IDE配置python语言进行编程
- Python每日一练——第16天:字符串分析
- Python爬取Boss直聘,帮你获取全国各类职业薪酬榜
- 没错!4 个 Python 函数就可以让你的数据框「骚气满满」!
- python 读不同编码的文本,传递一个可选的encoding 参数给open() 函数
- Python: 字符串格式化format()函数的使用
- python-Levenshtein几个计算字串相似度的函数解析
- Python @函数装饰器用法
- Python标准库:内置函数eval(expression, globals=None, locals=None)
- Python标准库:内置函数filter(function, iterable)
- python里怎么样通过函数名称来获取函数地址
- Python 和Java 哪个更适合做自动化测试?
- 【异常】前端ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.