首页 / PYTHON / Python学习笔记3-字符串
Python学习笔记3-字符串
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python学习笔记3-字符串,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2266字,纯文字阅读大概需要4分钟。
内容图文
![Python学习笔记3-字符串](/upload/InfoBanner/zyjiaocheng/1111/ca356fc4751c4e1d85c1ed017db1f51f.jpg)
格式化字符串/复合字段名
>>> import humansize >>> si_suffixes = humansize.SUFFIXES[1000] >>> si_suffixes [‘KB‘, ‘MB‘, ‘GB‘, ‘TB‘, ‘PB‘, ‘EB‘, ‘ZB‘, ‘YB‘] >>> ‘1000{0[0]} = 1{0[1]}‘.format(si_suffixes) ‘1000KB = 1MB‘
>>> import humansize >>> import sys >>> ‘1MB = 1000{0.modules[humansize].SUFFIXES[1000][0]}‘.format(sys) ‘1MB = 1000KB‘
Sys.modules 是一个保存当前python实例中搜有已导入模块的字典。模块的名字为键,模块自身为值。
>>> s = ‘‘‘finished files are the re- sults of years of scientific study combined with the experience of years. ‘‘‘ >>> s.splitlines() [‘finished files are the re-‘, ‘sults of years of scientific study combined with the‘, ‘experience of years. ‘] >>> print(s.lower()) finished files are the re- sults of years of scientific study combined with the experience of years.
>>> a_list = query.split("&") >>> a_list [‘user=pilgrim‘, ‘database=master‘, ‘password=PapayaWhip‘] >>> a_list_of_list = [v.split(‘=‘,1) for v in a_list] >>> a_list_of_list [[‘user‘, ‘pilgrim‘], [‘database‘, ‘master‘], [‘password‘, ‘PapayaWhip‘]] >>> a_dict = dict(a_list_of_list) >>> a_dict {‘password‘: ‘PapayaWhip‘, ‘database‘: ‘master‘, ‘user‘: ‘pilgrim‘}
split()-根据指定的分隔符,将字符串分隔成一个字符串列表。
dict() - 将包含列表的列表转换成字典对象
字符串的分片
>>> a_string = "My alphabet starts where your alphabet ends." >>> a_string[3:11] ‘alphabet‘ >>> a_string[3:-3] ‘alphabet starts where your alphabet en’ >>> a_string[:18] ‘My alphabet starts’ >>> a_string[18:] ‘ where your alphabet ends.‘
String VS. Bytes
Bytes对象的定义:b’ ’, eg: by = b’abcd\x65’
Bytes对象不能改变其值,但可以通过内置函数bytearry()将bytes对象转化成bytearry对象,bytearry对象的值可改变
>>> by = b‘abcd\x65‘ >>> barr = bytearray(by) >>> barr bytearray(b‘abcde‘) >>> barr[0]=102 >>> barr bytearray(b‘fbcde‘)
>>> a_string = "dive into python" >>> by = a_string.encode(‘utf-8‘) >>> by b‘dive into python‘ >>> roundtrip = by.decode(‘big5‘) >>> roundtrip ‘dive into python‘
string.encode() -- 使用某种编码方式作为参数,将字符串转化为bytes对象。
bytes.decode() -- 使用某种编码方式作为参数,将bytes对象转化成字符串对象。
原文:http://www.cnblogs.com/summerlong/p/4476010.html
内容总结
以上是互联网集市为您收集整理的Python学习笔记3-字符串全部内容,希望文章能够帮你解决Python学习笔记3-字符串所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。