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 ...
随机推荐
- tomcat RMI 停不掉
项目采用jfinal框架,做了一个RMI的服务,对其它程序提供服务.实现上,写了一个RmiPlugin package com.wisdombud.cloudtalk.plugin; import j ...
- ecshop 模板开发总结
ecshop 模板开发总结 模板标签 1.{$articleTitle|escape:"html"} 描述:用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码 ...
- SQLServer 数据库镜像+复制方案
目标: 主机做了Mirror和Replication,当主机出现问题时,Replication和Mirror实现自动的故障转移(Mirror 和Replication都切换到备机,而当主机 重新启动后 ...
- Core Java 总结(数据类型,表达式问题)
2016-10-18 整理 写一个程序判断整数的奇偶 public static boolean isOdd(int i){ return i % 2 == 1; } 百度百科定义:奇数(英文:odd ...
- Linux驱动开发——pr_fmt的用法
作者:彭东林 邮箱:pengdonglin137@163.com 在阅读kernel代码的时候,总是看到有很多驱动都在第一行定义pr_fmt,闲来没事,分析了一下, 发现,确实挺方便的.下面记录分享一 ...
- android使用PullToRefresh实现上拉加载和下拉刷新效果
其实很早前就在博客园中也写过官方的下拉刷新控件SwipeRefreshLayout,但是这个控件仅仅支持下拉刷新,用起来还算可以.然而在我们实际开发应用中,很多地方都不止有下拉刷新,而且还有上拉加载的 ...
- Windows下Git多账号配置,同一电脑多个ssh-key的管理
这一篇文章是对上一篇文章<Git-TortoiseGit完整配置流程>的拓展,所以需要对上一篇文章有所了解,当然直接往下看也可以,其中也有一些提到一些基础的操作. <Git-Tort ...
- 安装nodejs express框架时express命令行无效
我也是看了这篇才明白.http://jingyan.baidu.com/article/922554468a3466851648f419.html 最近在看一本书,nodejs开发指南.至于出现这个问 ...
- C#——this关键字(1)
//我的C#是跟着猛哥(刘铁猛)(算是我的正式老师)<C#语言入门详解>学习的,微信上猛哥也给我讲解了一些不懂得地方,对于我来说简直是一笔巨额财富,难得良师! 在学习C#的时候,老师讲的示 ...
- querySelectorAll 方法相比 getElementsBy 系列方法区别
最近有人问到querySelectorAll 方法相比 getElementsBy 系列方法区别,一时没想起来说些什么,今天查下文档,总结一下它们的区别,以便自己理解. 1. W3C 标准queryS ...