==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. 解决Sqlserver 2008 R2在创建登录名出错"此版本的 Microsoft Windows 不支持 MUST_CHANGE 选项。 (Microsoft SQL Server,错误: 15195)"

    错误信息:   执行 Transact-SQL 语句或批处理时发生了异常. (Microsoft.SqlServer.ConnectionInfo)   此版本的 Microsoft Windows ...

  2. 如何将数据转换libsvm格式文件

    原文:http://blog.sina.com.cn/s/blog_5c2f929b0100qse8.html 有三种工具可用1.网上有一个xls文FormatDataLibsvm.xls具有宏命令, ...

  3. [Node.js]27. Level 5: URL Building & Doing the Request

    Let's create a page which calls the twitter search API and displays the last few results for Code Sc ...

  4. spark shuffle过程分析

    spark shuffle流程分析 回到ShuffleMapTask.runTask函数 如今回到ShuffleMapTask.runTask函数中: overridedef runTask(cont ...

  5. 【Nodejs】“快算24”扑克牌游戏算法

    算24是一款扑克牌游戏,它的游戏方式是把四张牌的牌面数值通过四则运算得到结果24,四张牌必须仅用一次.这是一种挺好的锻炼孩子算数能力的扑克牌游戏. 各地玩法还有点差别,有的只算1-10,其它抽出来:有 ...

  6. JavaScript 之 日常积累

    1. <a>标签"加入收藏",兼容IE,FireFox等 function bookmarksite() { if (window.sidebar) { // Mozi ...

  7. 算法笔记_194:历届试题 翻硬币(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 小明正在玩一个“翻硬币”的游戏. 桌上放着排成一排的若干硬币.我们用 * 表示正面,用 o 表示反面(是小写字母,不是零). 比如,可能情 ...

  8. python 读帧和绘图的区别

    capture = cv2.VideoCapture(0) while True: #img = cv.QueryFrame(capture) ret, frame = capture.read() ...

  9. LightOj 1123-Trail Maintenance(最小生成树:神级删边)

    1123 - Trail Maintenance PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...

  10. 在OpenERP8.0中如何激活及时通讯功能im

    How to activate chat (im) in v8 (trunk) I know its already answered that chat (im) is only available ...