Python学习笔记3-字符串
格式化字符串/复合字段名
>>> 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对象转化成字符串对象。
Python学习笔记3-字符串的更多相关文章
- python学习笔记(字符串操作、字典操作、三级菜单实例)
字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...
- Python学习笔记3—字符串
原始字符串 使用\转义或者r,这种方法在网站设置网站目录结构的时候非常管用. >>> dos="c:\news" >>> print dos c ...
- 【Python学习笔记】字符串操作
字符串的表示 python中的字符串是一个常量,可以使用单引号'',双引号""或三引号""" """来创建一个字符串常量 ...
- Python学习笔记(3)-字符串
创建字符串 一对单引号或双引号 >>> 'hello world' 'hello world' >>> "hello world" 'hello ...
- Python学习笔记:字符串
字符串 字符串定义:字符串可以使用一对单引号.双引号或三引号来定义,即便是单个字符也会当做字符串来处理(Python中没有字符类型,单个字符也就是只有一个字符的字符串而已). 原始字符串:字符串中反斜 ...
- python学习笔记(一)---字符串与列表
字符串的一些处理 字符串的大小写 name="lonmar hb" print(name.upper())#全大写 print(name.lower())#全小写 print(na ...
- 【Python学习笔记】字符串拼接方法(5种)总结
字符串的 5 种拼接方法: “+”号 “,”号 直接连接 格式化 多行字符串拼接 第一种:“+”号 print("Hello"+"Python") 打印结果: ...
- python学习笔记(二)-字符串方法
python的字符串内建函数: #====================常用方法=============================name = 'besttest' new_name = n ...
- Python学习笔记----操作字符串
1.字符串相加.列表相加.列表和字符串不能混着使用 #序列相加 a="hello" b="python" c=a+b print("字符串相加的结果& ...
- Python学习笔记之字符串
一.字符串格式化 >>> format="Hello,%s. %s enough for ya?" >>> values=('World','H ...
随机推荐
- C#经典笔试题-获取字符串中相同的字符以及其个数
public Dictionary<char,int> GetStrSameAs(string str){ //将字符串转换成一个字符数组. char[] charArray=str.To ...
- python 三元运算
C:\Users\Administrator>pythonPython 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.190 ...
- Asp.Net 将枚举类型(enum)绑定到ListControl(DropDownList)控件
在开发过程中一些状态的表示使用到枚举类型,那么如何将枚举类型直接绑定到ListControl(DropDownList)是本次的主题,废话不多说了,直接代码: 首先看工具类代码: /// <su ...
- ★Kali信息收集~ 1.Google Hacking + Github Hacking
一.google hacking site site:cnblogs.com 毒逆天 intitle intitle:login allintitle allintitle:index of alli ...
- Hybrid App技术批量制作APP应用与跨平台解决方案
前言 简单的聊一聊我开发了4年之久的Hybrid App(混合模式移动应用)平台开发,目前一直在持续开发与维护,支持无编程快速开发! 其本意也不是要吹捧前端有多么强大,只是用自己的实际项目阐述下对于前 ...
- AngularJS开发指南11:AngularJS的model,controller,view详解
model model这个词在AngularJS中,既可以表示一个(比如,一个叫做phones的model,它的值是一个包含多个phone的数组)对象,也可以表示应用中的整个数据模型,这取决于我们所讨 ...
- Oracle 11g DG配置简明版
环境: 主库A机:在线生产环境,RHEL 6.4 + Oracle 11.2.0.3 备库B机:新增备机,RHEL 6.4 需求: 对生产环境最小影响前提下配置DG备库. 目录: 一.B机安装相同版本 ...
- 现代3D图形编程学习-环境设置
本书系列 现代3D图形编程学习 环境设置 由于本书中的例子,均是基于OpenGL实现的,因此你的工作环境需要能够运行OpenGL,为了读者能够更好的运行原文中的示例,此处简单地介绍了linux和win ...
- Linux 系统命令笔记
前言 翻出N年前学习笔记,感觉还有点用,放到博客备忘,自己查看用. 一. 系统命令笔记 1.系统 % /etc/issue # 查看操作系统版本 % # 观察系 ...
- 深入浅出数据仓库中SQL性能优化之Hive篇
转自:http://www.csdn.net/article/2015-01-13/2823530 一个Hive查询生成多个Map Reduce Job,一个Map Reduce Job又有Map,R ...