老办法
Python2.6之前,格式字符串的使用方法相对更简单些,虽然其能够接收的参数数量有限制。这些方法在Python3.3中仍然有效,但已有含蓄的警告称将完全淘汰这些方法,目前还没有明确的时间进度表。
格式化浮点数:
pi = 3.14159
print(" pi = %1.2f ", % pi)
多个替换值:
s1 = "cats" s2 = "dogs" s3 = " %s and %s living together" % (s1, s2)
没有足够的参数:
使用老的格式化方法,我经常犯错"TypeError: not enough arguments for formating string",因为我数错了替换变量的数量,编写如下这样的代码很容易漏掉变量。
立即学习“Python免费学习笔记(深入)”;
本文档主要讲述的是Android数据格式解析对象JSON用法;JSON可以将Java对象转成json格式的字符串,可以将json字符串转换成Java。比XML更轻量级,Json使用起来比较轻便和简单。JSON数据格式,在Android中被广泛运用于客户端和服务器通信,在网络数据传输与解析时非常方便。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看
0
set = (%s, %s, %s, %s, %s, %s, %s, %s) " % (a,b,c,d,e,f,g,h,i)
对于新的Python格式字符串,可以使用编号的参数,这样你就不需要统计有多少个参数。
set = set = " ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}) ".format(a,b,c,d,e,f,g)
Python 2.x 基于字典字符串格式化
"%(n)d %(x)s" %{"n":1, "x":"spam"}
reply = """
Greetings...
Hello %(name)s!
Your age squared is %(age)s
"""
values = {'name':'Bob', 'age':40}
print rely % values
Python 3.x format方法格式化
template = '{0},{1} and {2}'
template.format('spam','ham','eggs')
template = '{motto}, {pork} and {food}'
template.format(motto='spam', pork='ham', food='eggs')
template = '{motto}, {0} and {food}'
template.format('ham', motto='spam', food='eggs')
'{motto}, {0} and {food}'.format(42, motto=3.14, food=[1,2,3])
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号