您的位置:首页 > 编程语言 > Python开发

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代表格式化的操作

字典健:< > =或^

可变类型:列表,字典,可变集合

不可变类型:数字,字符串,元组,不可变集合
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: