==string 模块==

``string`` 模块提供了一些用于处理字符串类型的函数,
如 [Example 1-51 #eg-1-51] 所示. ====Example 1-51. 使用 string 模块====[eg-1-51] ```
File: string-example-1.py import string text = "Monty Python's Flying Circus" print "upper", "=>", string.upper(text)
print "lower", "=>", string.lower(text)
print "split", "=>", string.split(text)
print "join", "=>", string.join(string.split(text), "+")
print "replace", "=>", string.replace(text, "Python", "Java")
print "find", "=>", string.find(text, "Python"), string.find(text, "Java")
print "count", "=>", string.count(text, "n") *B*upper => MONTY PYTHON'S FLYING CIRCUS
lower => monty python's flying circus
split => ['Monty', "Python's", 'Flying', 'Circus']
join => Monty+Python's+Flying+Circus
replace => Monty Java's Flying Circus
find => 6 -1
count => 3*b*
``` 在 Python 1.5.2 以及更早版本中, ``string`` 使用 ``strop`` 中的函数来实现模块功能. 在 Python1.6 和后继版本,更多的字符串操作都可以作为字符串方法来访问,
如 [Example 1-52 #eg-1-52] 所示, ``string`` 模块中的许多函数只是对相对应字符串方法的封装. ====Example 1-52. 使用字符串方法替代 string 模块函数====[eg-1-52] ```
File: string-example-2.py text = "Monty Python's Flying Circus" print "upper", "=>", text.upper()
print "lower", "=>", text.lower()
print "split", "=>", text.split()
print "join", "=>", "+".join(text.split())
print "replace", "=>", text.replace("Python", "Perl")
print "find", "=>", text.find("Python"), text.find("Perl")
print "count", "=>", text.count("n") *B*upper => MONTY PYTHON'S FLYING CIRCUS
lower => monty python's flying circus
split => ['Monty', "Python's", 'Flying', 'Circus']
join => Monty+Python's+Flying+Circus
replace => Monty Perl's Flying Circus
find => 6 -1
count => 3*b*
``` 为了增强模块对字符的处理能力, 除了字符串方法, ``string``
模块还包含了类型转换函数用于把字符串转换为其他类型, (如 [Example 1-53 #eg-1-53] 所示). ====Example 1-53. 使用 string 模块将字符串转为数字====[eg-1-53] ```
File: string-example-3.py import string print int(""),
print string.atoi(""),
print string.atoi("", 8), # octal 八进制
print string.atoi("", 16), # hexadecimal 十六进制
print string.atoi("3mv", 36) # whatever... print string.atoi("", 0),
print string.atoi("", 0),
print string.atoi("0x4711", 0) print float(""),
print string.atof(""),
print string.atof("1.23e5") *B*4711 4711 4711 4711 4711
4711 2505 18193
4711.0 1.0 123000.0*b*
``` 大多数情况下 (特别是当你使用的是1.6及更高版本时) ,你可以使用 ``int`` 和 ``float``
函数代替 ``string`` 模块中对应的函数。 ``atoi`` 函数可以接受可选的第二个参数, 指定数基(number base).
如果数基为 0, 那么函数将检查字符串的前几个字符来决定使用的数基:
如果为 "0x," 数基将为 16 (十六进制), 如果为 "0," 则数基为 8 (八进制).
默认数基值为 10 (十进制), 当你未传递参数时就使用这个值. 在 1.6 及以后版本中, ``int`` 函数和 ``atoi`` 一样可以接受第二个参数.
与字符串版本函数不一样的是 , ``int`` 和 ``float`` 可以接受 Unicode 字符串对象.

python标准库介绍——4 string模块详解的更多相关文章

  1. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  2. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  3. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  4. python标准库介绍——30 code 模块详解

    ==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...

  5. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  6. python标准库介绍——36 popen2 模块详解

    ==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...

  7. python标准库介绍——19 mmap 模块详解

    ==mmap 模块== (2.0 新增) ``mmap`` 模块提供了操作系统内存映射函数的接口, 如 [Example 2-13 #eg-2-13] 所示. 映射区域的行为和字符串对象类似, 但数据 ...

  8. python标准库介绍——18 StringIO 模块详解

    ==StringIO 模块== [Example 2-8 #eg-2-8] 展示了 ``StringIO`` 模块的使用. 它实现了一个工作在内存的文件对象 (内存文件). 在大多需要标准文件对象的地 ...

  9. python标准库介绍——13 types 模块详解

    == types 模块== ``types`` 模块包含了标准解释器定义的所有类型的类型对象, 如 [Example 1-86 #eg-1-86] 所示. 同一类型的所有对象共享一个类型对象. 你可以 ...

随机推荐

  1. 用C语言获取任意文件的长度(可能大于2GB)

    用C语言获取文件长度的常见思路是: 打开文件后用 fseek() 函数把文件位置指针移动到文件的末尾,用 ftell() 获得这时位置指针距文件头的字节数,这个字节数就是文件的长度.但是这样做也会受到 ...

  2. NTP Server

    Network Time Protocol互联网时间协议 NTP is intended to synchronize all participating computers to within a ...

  3. Ngxtop-Nginx日志实时分析利器

    ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop.有了ngxtop,你可以实时了解到当前nginx的访问状况,再也不需要tail ...

  4. nodejs pm2的简单应用

    一.简介 pm2是一个带有负载均衡功能的应用进程管理器,类似有Supervisor,forever,详细参数见官网:http://pm2.keymetrics.io 二.安装 Linux Binari ...

  5. COM中的HRESULT

  6. 【leetcode 桶排序】Maximum Gap

    1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...

  7. python input 与raw_input函数的区别

    转自:http://blog.csdn.net/sruru/article/details/7790436 以前没有深入考虑过raw_input与input函数的区别,所以一直比较困惑,今天测试之后, ...

  8. PCL系列——怎样逐渐地配准一对点云

    欢迎訪问 博客新址 PCL系列 PCL系列--读入PCD格式文件操作 PCL系列--将点云数据写入PCD格式文件 PCL系列--拼接两个点云 PCL系列--从深度图像(RangeImage)中提取NA ...

  9. 关于支付宝即时到帐异步通知(notify_url)一点总结

    (1)首先做支付的商业网站,需要能够上网(支付成功后,需要进行参数回传验证,如果上不了网,responseText就直接为false)(2)notify_url这个不能进行验证,比如继承父类Page, ...

  10. awk的使用

    http://www.cnblogs.com/chengmo/archive/2010/10/08/1845913.html linux awk 内置函数详细介绍(实例) awk内置字符串函数 awk ...