PYTHON string format

1. in python3 use dict to formate strings:

>>>print(“%(a)s” % {'a’:'b’})

b

>>>a = ‘b’

>>>print(“%(a)s” % locals())

b

2. in Python 2.6 on up you can use the new format() method on strings:

>>>'{a}'.format(a = ‘spam’)

'spam’

3. using string

>>>import string

>>>template = string.Template(“$scheme://$host:$port/$route#$fragment”)

>>>template.substitute(scheme = ”http”, host = ”google.com”, port = “80”, route = “”, fragement = “”)

'http://google.com:80/#

posted @ 2011-06-20 20:28  Mingxx  阅读(1328)  评论(0)    收藏  举报