python string.atof( )函数
2016-05-13 21:56
405 查看
string.atof(s)将字符串转为浮点型数字
string.atoi(s,[base=num])将字符串转为整型数字,base 指定进制
string.capwords(s,sep=None)以sep作为分隔符,分割字符串s,然后将每个字段的首字母换成大写
string.maketrans(s,r)创建一个s到r的转换表,然后可以使用translate()方法来使用
>>> string.atof("1.23") 1.23 >>> string.atof("1") 1.0
string.atoi(s,[base=num])将字符串转为整型数字,base 指定进制
>>> string.atoi("20") 20 >>> string.atoi("20",base=10) 20 >>> string.atoi("20",base=16) 32 >>> string.atoi("20",base=8) 16 >>> string.atoi("20",base=2) Traceback (most recent call last): File "", line 1, in <module> File "/usr/lib64/python2.6/string.py", line 403, in atoi return _int(s, base) ValueError: invalid literal for int() with base 2: '20' >>> string.atoi("101",base=2) 5 >>> string.atoi("101",base=6) 37
string.capwords(s,sep=None)以sep作为分隔符,分割字符串s,然后将每个字段的首字母换成大写
>>> string.capwords("this is a dog") 'This Is A Dog' >>> string.capwords("this is a dog",sep=" ") 'This Is A Dog' >>> string.capwords("this is a dog",sep="s") 'This is a dog' >>> string.capwords("this is a dog",sep="o") 'This is a doG' >>>
string.maketrans(s,r)创建一个s到r的转换表,然后可以使用translate()方法来使用
>>> replist=string.maketrans("123","abc") >>> replist1=string.maketrans("456","xyz") >>> s="123456789" >>> s.translate(replist) 'abc456789' >>> s.translate(replist1) '123xyz789'
相关文章推荐
- Python闲谈(一)mgrid慢放
- python urllib 和urllib2的区别
- Python标准输出重定向
- Windows下打包Python的exe可执行文件
- Python之路—Day1作业
- python3.x输出文件最后几行
- pip install jupyter UnicodeDecodeError | ipython notebook 安装失败
- Python――How to use python pip/pur
- 如何上传python
- 如何上传python
- 最全Python爬虫总结
- Beautiful Soup 4库--python2.x(学习日记)
- Python实现欧拉计划23题
- Python之路—Day1
- 如何优雅的用Python玩转语音聊天机器人
- leetcode(8),Majority Element(python)
- python的import详解
- python的import详解
- python内置函数第四讲(2)
- python - num1 -初识python