==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 字符串对象.

最新文章

  1. Select2个人使用总结
  2. lightbox图片展示效果
  3. 纯CSS多级菜单
  4. studio adb连接不上手机 ADB server didn't ACK
  5. VirtualBox内Linux系统与Windows共享文件夹
  6. 20169210《Linux内核原理与分析》第七周作业
  7. 动态树 Link-Cut Trees
  8. CANoe 入门 Step by step系列(二)CAPL编程【转】
  9. windows 版Tomcat 7.0的配置
  10. Java NIO——2 缓冲区
  11. Ibatis 3.0 之前使用的都是2.0 3.0与2.0的内容有很大的不同
  12. 自动化运维:使用flask+mysql+highcharts搭建监控平台
  13. ABP拦截器之UnitOfWorkRegistrar(一)
  14. 敏捷开发——User Story
  15. XV Open Cup named after E.V. Pankratiev. GP of Siberia-Swimming
  16. PCL-安装
  17. mysql实现简单的增删改查,放入xmapp自带数据库中
  18. IDEA的十大快捷键
  19. 登录之md5加密
  20. StartCoroutine 和 StopCoroutine

热门文章

  1. angular5使用httpclient时解决跨域问题
  2. NSURLSession 学习笔记
  3. 心电图html js控件
  4. 【C++系列小结】面向过程的编程风格
  5. Swift学习笔记(十五)——程序猿浪漫之用Swift+Unicode说我爱你
  6. SQL Sever 2008配置工具中过程调用失败解决方法
  7. rEFind 教程
  8. tensorflow serving 打印调试log
  9. Python之str方法
  10. linux上传下载文件rz,sz