后端开发|Python教程
python
后端开发-Python教程
源码3.29,vscode报不能保存,ubuntu 网络助手,tomcat清缓存配置,爬虫神通,阿里云部署php,seo需要什么代码,asp最新表白网站源码下载,简单淘宝html模板lzw
固定资产软件 源码下载,ubuntu14.04显卡,怎么把tomcat装成服务,爬虫抓json包,php冒泡排序的复杂度,seo基础网站lzw
python中format函数什么意思?
java 支付源码,vscode账户登录,ubuntu),tomcat纯驱动,sqlite 手机版软件,手机端 图片上传插件,前端框架简笔画简单绘画,爬虫工具脚本违规吗,php取首字母,铁西优化seo,网站浮窗代码,网页禁止右键,javascript 模板系统lzw
Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。
基本语法是通过 {} 和 : 来代替以前的 % 。
format 函数可以接受不限个参数,位置可以不按顺序。
推荐:《Python教学》
实例
>>>"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序hello world >>> "{0} {1}".format("hello", "world") # 设置指定位置hello world >>> "{1} {0} {1}".format("hello", "world") # 设置指定位置world hello world
也可以设置参数:
实例
#!/usr/bin/python# -*- coding: UTF-8 -*- print("网站名:{name}, 地址 {url}".format(name="菜鸟教学", url="")) # 通过字典设置参数site = {"name": "菜鸟教学", "url": ""}print("网站名:{name}, 地址 {url}".format(**site)) # 通过列表索引设置参数my_list = [菜鸟教学, \]print("网站名:{0[0]}, 地址 {0[1]}".format(my_list)) # "0" 是必须的
输出结果为:
网站名:菜鸟教学, 地址 网站名:菜鸟教学, 地址 网站名:菜鸟教学, 地址
也可以向 str.format() 传入对象:
实例
#!/usr/bin/python# -*- coding: UTF-8 -*- class AssignValue(object): def __init__(self, value): self.value = valuemy_value = AssignValue(6)print(value 为: {0.value}.format(my_value)) # "0" 是可选的
输出结果为:
value 为: 6