>>> 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. atoi函数的实现——面试

    主要考虑,字符串中是否有非法字符,字符串是否有溢出控制 #include<stdio.h> int myatoi(const char *str){ ,ret=,i=; if(str[i] ...

  2. 【备忘录】CentOS服务器mysql忘记root密码恢复

    mysql的root忘记,现无法操作数据库 停止mysql服务service  mysql stop 然后使用如下的参数启动mysql, --skip-grant-tables会跳过mysql的授权 ...

  3. REST API权限集成设计

    REST API权限集成设计 应用分为两大部分,前端html+后端Rest服务,前端html和后端Rest服务部署完全分离. 目标:可访问资源都处于权限控制之下(意味着通过浏览器地址栏的任意url都会 ...

  4. cpp分解质因数

    原理有点像埃氏筛. #include <stdio.h> #include <iostream> #include <stdlib.h> using namespa ...

  5. python 文件操作的函数

    1. 文件操作的函数 open(文件名(路径), mode="?", encoding="字符集") 2. 模式: r, w, a, r+, w+, a+, r ...

  6. Java-Runoob:Java Character 类

    ylbtech-Java-Runoob:Java Character 类 1.返回顶部 1. Java Character 类 Character 类用于对单个字符进行操作. Character 类在 ...

  7. H3C V7版本的系统默认权限

    H3C (v7平台)Console口通过账号密码登陆配置教程 http://www.023wg.com/h3c/496.html H3C (v7平台)Console口通过账号密码登陆配置教程 看不懂的 ...

  8. Go - 基础知识

    经历了五一小假期,前后差不多一周多没有坚持学习了,所以在归来的第一时间继续 Go 的学习之旅. Go 程序的基本结构 首先先贴出一段简单的代码:HelloGo.go // HelloGo packag ...

  9. Python+Selenium爬虫实战一《将QQ今日话题发布到个人博客》

    前提条件: 1.使用Wamp Server部署WordPress个人博客,网上资料较多,这里不过多介绍 思路: 1.首先qq.com首页获取到今日话题的的链接: 2.通过今日话题链接访问到今日话题,并 ...

  10. PHP mysql client封装

    config1.inc.php $CONFIG_DATABASE_TXL = array( #array('127.0.0.1', 'root', '', 'he_txl','3306') array ...