python学习手册笔记--第7章--字符串
2012-02-26 18:13
453 查看
单引号和双引号通用
path=r'c:\new\t' r+''关闭转义机制
r''不能以单个\结尾 可以:(r'\\'[:-1])
"""......."""多行模块
substr in str 判断子串是否在串中
分片
s[-2] 支持负偏移
s[:] 实现顶层拷贝
s[1:10:2] 2是步进 s[::-1]即为翻转字符串<==>s[slice(None,None,-1)]
转换
ascII码转换 ord('s')>>>115 chr(115)>>>'s'
list->string:
l=['a','b'] s=''.join(l)>>'ab'
文本解析:
line='aaa bbb ccc'
col=line.split()>>>['aaa', 'bbb', 'ccc'] 默认是空格分割
.rstrip() 去掉'\n'
.upper() 变大写
.statrswith('**')/.endswith('**') 判断开头/结尾
字符串格式化表达式:
一版都用%s即可
%[name][flags][width][.precision]typecode width/precision可以通过指定
'that is %d %s bird' % (1, 'red') >>> 'that is 1 red bird'
基于字典的字符串格式化:
'%(name) hello'%{"name":'cai'}>>>'cai hello'
内置函数vars(),包含调用时存在的变量及对应的值
'this is {0} {2} bird'.format(1,'red')>>>'this is 1 red bird' <==> 'this is {0} {col} bird'.format(1, col='red')
'my {1[hei]} runs {0.platporm}'.format(sys, {'hei':'laptop'})>>>'my laptop runs linux2'
{**:XX}XX代表格式化的操作
字典健:< > =或^
可变类型:列表,字典,可变集合
不可变类型:数字,字符串,元组,不可变集合
path=r'c:\new\t' r+''关闭转义机制
r''不能以单个\结尾 可以:(r'\\'[:-1])
"""......."""多行模块
substr in str 判断子串是否在串中
分片
s[-2] 支持负偏移
s[:] 实现顶层拷贝
s[1:10:2] 2是步进 s[::-1]即为翻转字符串<==>s[slice(None,None,-1)]
转换
ascII码转换 ord('s')>>>115 chr(115)>>>'s'
list->string:
l=['a','b'] s=''.join(l)>>'ab'
文本解析:
line='aaa bbb ccc'
col=line.split()>>>['aaa', 'bbb', 'ccc'] 默认是空格分割
.rstrip() 去掉'\n'
.upper() 变大写
.statrswith('**')/.endswith('**') 判断开头/结尾
字符串格式化表达式:
一版都用%s即可
%[name][flags][width][.precision]typecode width/precision可以通过指定
'that is %d %s bird' % (1, 'red') >>> 'that is 1 red bird'
基于字典的字符串格式化:
'%(name) hello'%{"name":'cai'}>>>'cai hello'
内置函数vars(),包含调用时存在的变量及对应的值
'this is {0} {2} bird'.format(1,'red')>>>'this is 1 red bird' <==> 'this is {0} {col} bird'.format(1, col='red')
'my {1[hei]} runs {0.platporm}'.format(sys, {'hei':'laptop'})>>>'my laptop runs linux2'
{**:XX}XX代表格式化的操作
字典健:< > =或^
可变类型:列表,字典,可变集合
不可变类型:数字,字符串,元组,不可变集合
相关文章推荐
- python学习手册 第7章 字符串
- Python学习笔记5-字符串、bool、数值操作和数组字典排序
- python学习笔记——第三章 字符串
- python cookbook 学习笔记 -- 1.7 将字符串逐字符或逐词反转
- Python学习笔记-数字&字符串
- python字符串格式化之学习笔记
- [学习笔记]Python_字符串,元祖,字典
- Python学习笔记(五):字符串
- Python学习笔记(4),字符串没有append用法
- Python学习笔记之疑问10:如何使用分隔符连接list中的字符串
- Python 学习笔记 - 字符串的格式化
- python学习笔记五:字符串方法
- Python学习笔记--Python字符串连接方法总结
- Python学习笔记——字符串
- Python学习笔记八:字符串的操作(二)
- 【Python 学习手册笔记】介绍Python对象类型
- Python学习笔记 - 字符串和编码
- Python学习笔记(五)-- Python字符串
- Python学习笔记(二):基本数据类型及操作(逻辑、字符串、浮点、复数)
- python学习的思维导图笔记--字符串