python~string常量及模板

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37579123/article/details/81533156

字符串常量

>>> import string

>>> string.ascii_uppercase

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

 

>>> string.ascii_lowercase

'abcdefghijklmnopqrstuvwxyz'

 

>>> string.ascii_letters

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

>>> string.ascii_lowercase+string.ascii_uppercase

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

 

>>> string.digits

'0123456789

 

>>> string.hexdigits

'0123456789abcdefABCDEF'

 

>>> string.octdigits

'01234567'

 

>>> string.punctuation

'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

 

>>> string.printable

'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

 

>>> string.whitespace

' \t\n\r\x0b\x0c'

以上是py3中的字符串常量,py2与之不同的是:

>>> string.lowercase

'abcdefghijklmnopqrstuvwxyz'

>>> string.uppercase

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

>>> string.letters

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

 

字符串模板

Template

>>> from string import Template

>>> new_str=Template("There are ${key1} ${key2}")

>>> new_str.substitute(key1="3",key2="python")

'There are 3 python'

 

>>> from string import Template

>>> s={"key":"dogs"}

>>> t=Template("There are ${key}")

>>> t.substitute(s)

'There are dogs'

猜你喜欢

转载自blog.csdn.net/weixin_37579123/article/details/81533156