时间:2025-04-15 07:00:32
format使用方法
format的使用方法主要有以下几种:
1. 基本用法:
- 按照{}的顺序依次匹配括号中的值。例如:s = "%s is a %s" % ("Tom", "Boy") 。
- 通过索引的方式去匹配参数。例如:s = "%s is a %s" % ("Tom", "Boy") 。
- 通过参数名来匹配参数。例如:s = "%(name)s is a %(sex)s" % {"name": "Tom", "sex": "Boy"} 。
- 混搭使用。可以通过索引,参数名来混搭进行匹配。例如:s = "My name is %s, i am %s year old, She name is %s" % ("Liming", "Lily", age=10) 。
2. 进阶用法:
- 通过对象的属性。例如:class Names: name1 = "Kevin"; name2 = "Tom"; print "hello %s i am %s" % (Names.name1, Names.name2) 。
- 使用Format函数。例如:Format(expression[,format[,firstdayofweek[,firstweekofyear]]]) 。
快测评广州东远堂信息科技有限公司版权所有 网站地图提供支持 粤ICP备15011623号