python标准库介绍——4 string模块详解
==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模块详解的更多相关文章
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python标准库介绍——10 sys 模块详解
==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...
- python标准库介绍——30 code 模块详解
==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...
- python标准库介绍——8 operator 模块详解
==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...
- python标准库介绍——36 popen2 模块详解
==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...
- python标准库介绍——19 mmap 模块详解
==mmap 模块== (2.0 新增) ``mmap`` 模块提供了操作系统内存映射函数的接口, 如 [Example 2-13 #eg-2-13] 所示. 映射区域的行为和字符串对象类似, 但数据 ...
- python标准库介绍——18 StringIO 模块详解
==StringIO 模块== [Example 2-8 #eg-2-8] 展示了 ``StringIO`` 模块的使用. 它实现了一个工作在内存的文件对象 (内存文件). 在大多需要标准文件对象的地 ...
- python标准库介绍——13 types 模块详解
== types 模块== ``types`` 模块包含了标准解释器定义的所有类型的类型对象, 如 [Example 1-86 #eg-1-86] 所示. 同一类型的所有对象共享一个类型对象. 你可以 ...
随机推荐
- c语言基础,\r, \n, \r\n
Enumeration (or enum) in C Enumeration (or enum) is a user defined data type in C. It is mainly used ...
- (转)<Unity3D>Unity3D在android下调试
转自:http://blog.csdn.net/zuoyamin/article/details/11827309 一.工具准备 1.JDK——由于android是基于Java平台开发的,jdk是必须 ...
- [Backbone] Verying Views
Below we have our AppointmentsView instance rendering and then taking the rendered HTML and insertin ...
- 自定义Lisp透明命令
我们知道在CAD中,如果我们在命令前面加一个单引号,则为透明命令.透明命令就是一个命令还没结束,中间插入另一个命令,然后继续完成前一个命令.插入的命令即透明命令,插入透明命令是为了更方便的完成第一个命 ...
- HDU4666+POJ2926【最远曼哈顿距离】
一开始就明白那个N*1<k的算法了, 可无奈删除操作耗时还是太多,最后学习了STL set,map相应的用法,方便好多. STL真的是一个好工具 #include<iostream> ...
- JSTL 标签库 使用(web基础学习笔记十九)
标签库概要: 一.C标签库介绍 1.1.<c:> 核心标签库 JSTL 核心标签库(C标签)标签共有13个,功能上分为4类:1.表达式控制标签:out.set.remove.catch2 ...
- interllij idea13 clone及push工程到github上
转自:http://my.oschina.net/okchen/blog/337556 为什么用Intelli J idea 而不是Eclipse?因为我早已无法忍受Eclipse的慢了,搞不好还会奔 ...
- javascript:void(0) 真正含义
大家常见这种代码: <a href="javascript:doTest2();void(0);">here</a> 但这儿的void(0)到底是何含义呢? ...
- SecureCRT 命令行备注
> 查出某个域名绑定的IP nslookup api.kaixin001.com Non-authoritative answer: Name: a.kaixin001.com Addre ...
- MySQL auto_increment_increment 和 auto_increment_offset
参考这一篇文章:(不过我对这一篇文章有异议) http://blog.csdn.net/leshami/article/details/39779509 1:搭建测试环境 create table t ...