>>> import string

 >>> string.ascii_letters

 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

 >>> string.ascii_lowercase

 'abcdefghijklmnopqrstuvwxyz'

 >>> string.ascii_uppercase

 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

 >>> string.digits

 ''

 >>> string.hexdigits

 '0123456789abcdefABCDEF'

 >>> string.letters

 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

 >>> string.lowercase

 'abcdefghijklmnopqrstuvwxyz'

 >>> string.uppercase

 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

 >>> string.octdigits

 ''

 >>> string.punctuation

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

 >>> string.printable

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

 >>> string.whitespace

 '\t\n\x0b\x0c\r

 >>> '{0}, {1}, {2}'.format('a', 'b', 'c')

 'a, b, c'

 >>> '{}, {}, {}'.format('a', 'b', 'c')  # 2.7+ only

 'a, b, c'

 >>> '{2}, {1}, {0}'.format('a', 'b', 'c')

 'c, b, a'

 >>> '{2}, {1}, {0}'.format(*'abc')      # unpacking argument sequence

 'c, b, a'

 >>> '{0}{1}{0}'.format('abra', 'cad')   # arguments' indices can be repeated

 'abracadabra'

 >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')

 'Coordinates: 37.24N, -115.81W'

 >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}

 >>> 'Coordinates: {latitude}, {longitude}'.format(**coord)

 'Coordinates: 37.24N, -115.81W'

 >>> c = 3-5j

 >>> ('The complex number {0} is formed from the real part {0.real} '

 ...  'and the imaginary part {0.imag}.').format(c)

 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'

 >>> class Point(object):

 ...     def __init__(self, x, y):

 ...         self.x, self.y = x, y

 ...     def __str__(self):

 ...         return 'Point({self.x}, {self.y})'.format(self=self)

 ...

 >>> str(Point(4, 2))

 'Point(4, 2)

 >>> coord = (3, 5)

 >>> 'X: {0[0]};  Y: {0[1]}'.format(coord)

 'X: 3;  Y: 5'

 >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')

 "repr() shows quotes: 'test1'; str() doesn't: test2"

 >>> '{:<30}'.format('left aligned')

 'left aligned                  '

 >>> '{:>30}'.format('right aligned')

 '                 right aligned'

 >>> '{:^30}'.format('centered')

 '           centered           '

 >>> '{:*^30}'.format('centered')  # use '*' as a fill char

 '***********centered***********'

 >>> '{:+f}; {:+f}'.format(3.14, -3.14)  # show it always

 '+3.140000; -3.140000'

 >>> '{: f}; {: f}'.format(3.14, -3.14)  # show a space for positive numbers

 ' 3.140000; -3.140000'

 >>> '{:-f}; {:-f}'.format(3.14, -3.14)  # show only the minus -- same as '{:f}; {:f}'

 '3.140000; -3.140000'

 >>> # format also supports binary numbers

 >>> "int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42)

 'int: 42;  hex: 2a;  oct: 52;  bin: 101010'

 >>> # with 0x, 0o, or 0b as prefix:

 >>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)

 'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'

 >>> '{:,}'.format(1234567890)

 '1,234,567,890'

 >>> points = 19.5

 >>> total = 22

 >>> 'Correct answers: {:.2%}.'.format(points/total)

 'Correct answers: 88.64%'

 >>> import datetime

 >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)

 >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)

 '2010-07-04 12:15:58'

 >>> for align, text in zip('<^>', ['left', 'center', 'right']):

 ...     '{0:{fill}{align}16}'.format(text, fill=align, align=align)

 ...

 'left<<<<<<<<<<<<'

 '^^^^^center^^^^^'

 '>>>>>>>>>>>right'

 >>>

 >>> octets = [192, 168, 0, 1]

 >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)

 'C0A80001'

 >>> int(_, 16)

 3232235521

 >>>

 >>> width = 5

 >>> for num in range(5,12):

 ...     for base in 'dXob':

 ...         print '{0:{width}{base}}'.format(num, base=base, width=width),

 ...     print

 ...

 5 5 5 101

 6 6 6 110

 7 7 7 111

 8 8 10 1000

 9 9 11 1001

 10 A    12 1010

 11 B    13 1011

 >>> from string import Template

 >>> s = Template('$who likes $what')

 >>> s.substitute(who='tim', what='kung pao')

 'tim likes kung pao'

 >>> d = dict(who='tim')

 >>> Template('Give $who $100').substitute(d)

 Traceback (most recent call last):

 [...]

 ValueError: Invalid placeholder in string: line 1, col 10

 >>> Template('$who likes $what').substitute(d)

 Traceback (most recent call last):

 [...]

 KeyError: 'what'

 >>> Template('$who likes $what').safe_substitute(d)

 'tim likes $what'

 string.capitalize(word) 返回一个副本,首字母大写

 >>> string.capitalize("hello")

 'Hello'

 >>> string.capitalize("hello world")

 'Hello world'

 >>> string.split("asdadada asdada")

 ['asdadada', 'asdada']

 >>> string.strip("              adsd         ")

 'adsd'

 >>> string.rstrip("              adsd         ")

 '              adsd'

 >>> string.lstrip("              adsd         ")

 'adsd         '

 string.swapcase(s) 小写变大写,大写变小写

 >>> string.swapcase("Helloo")

 'hELLOO'

 >>> string.ljust("ww",20)

 'ww                  '

 >>> string.rjust('ww',20)

 '                  ww'

 >>> string.center('ww',20)

 '         ww         '

 string.zfill(s, width)

 Pad a numeric string on the left with zero digits until the given width is reached. Strings starting with a sign are handled correctly.

 >>> string.zfill('ww',20)

 '000000000000000000ww'

Python String模块详解的更多相关文章

  1. 小白的Python之路 day5 random模块和string模块详解

    random模块详解 一.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 二.常用方法 1. random.r ...

  2. python time模块详解

    python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明  一.简介 ...

  3. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  4. python docopt模块详解

    python docopt模块详解 docopt 本质上是在 Python 中引入了一种针对命令行参数的形式语言,在代码的最开头使用 """ ""&q ...

  5. python pathlib模块详解

    python pathlib模块详解    

  6. Python Fabric模块详解

    Python Fabric模块详解 什么是Fabric? 简单介绍一下: ​ Fabric是一个Python的库和命令行工具,用来提高基于SSH的应用部署和系统管理效率. 再具体点介绍一下,Fabri ...

  7. python time 模块详解

    Python中time模块详解 发表于2011年5月5日 12:58 a.m.    位于分类我爱Python 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括: ...

  8. python标准库介绍——4 string模块详解

    ==string 模块== ``string`` 模块提供了一些用于处理字符串类型的函数, 如 [Example 1-51 #eg-1-51] 所示. ====Example 1-51. 使用 str ...

  9. python常用模块详解

    python常用模块详解 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用p ...

随机推荐

  1. 2.Python输入pip命令出现Unknown or unsupported command 'install'问题解决

    1.在学习python时,输入pip命令的时候出现以下错误: 2.原因:输入where pip命令查找,发现结果如下图,原因是因为电脑原先装了LoadRunner,导致系统无法识别应该使用哪一个pip ...

  2. webstorm配置scss的小结

    1)安装ruby 2)安装sass 3)配置webstorm 打开webstrom ->file->setting->Tools->file watcher 添加scss pr ...

  3. 第十一章 Helm-kubernetes的包管理器(上)

    Helm - K8s的包管理器 11.1 Why Helm K8s能够很好的组织和编排容器,但它缺少一个更高层次的应用打包工具,Helm就是干这个的. 比如对于一个MySQL服务,K8s需要部署如下对 ...

  4. 我编辑的JAVA日历程序

    class calendar { public static void main(String[]args) { int yearIn ; yearIn = Integer.parseInt(args ...

  5. 数据结构和算法之单向链表二:获取倒数第K个节点

    我们在做算法的时候或多或少都会遇到这样的问题,那就是我们需要获取某一个数据集的倒数或者正数第几个数据.那么今天我们来看一下这个问题,怎么去获取倒数第K个节点.我们拿到这个问题的时候自然而然会想到我们让 ...

  6. Git学习之常用的命令

    配置git git config --global user.name "你的github用户名" git config --global user.email "你的G ...

  7. Django学习---ajax

    Ajax 应用场景:我们在输入表单进行提交的时候往往会判断输入的数据形式是否正确,这个时候如果我们点击了提交就会刷新页面.如果我们不想要它刷新页面,让它“悄悄的提交数据”,这个时候我们就需要使用aja ...

  8. 浪潮openStack云

  9. ubuntu下永久修改DNS

    通过修改: sudo vi /etc/resolvconf/resolv.conf.d/base(这个文件默认是空的) 在里面插入: nameserver 8.8.8.8 nameserver 8.8 ...

  10. 将OCX控件打包成EXE,实现双击后自动注册<转>

    工具:2345好压[其他压缩软件应该大同小异] 第一步:首先将要打包的OCX控件,以及该控件所依赖的DLL文件放到桌面: 第二步:1.新建文本文档,取名 register.txt,文档内写入   re ...