Python笔记--第3章字符串(Python基础教程)

一、字符串基本操作

索引、切片、乘法、成员资格检查、长度、长度、最小值、最大值

字符串不可变,因此元素赋值和切片赋值是非法的、

二、设置字符串的格式

>>> "{3} {0} {2} {1} {3} {0}".format("be","not","or","to")

'to be or not to be'

三 设置字符串长度

>>> "{foo} {} {bar} {}".format(1,2,bar=4,foo=3)

'3 1 4 2'

>>> "{foo} {1} {bar} {0}".format(1,2,bar=4,foo=3)

'3 2 4 1'

四 字符串方法 

1.center

>>> "WangTianqi".center(66)

'                            WangTianqi                            '

>>> "WangTianqi".center(66,"*")

'****************************WangTianqi****************************'

2.find

>>>"WangTianqi".find('an')

1

3.join

>>> seq='+'

>>> sep=['1','2']

>>> seq.join(sep)

'1+2'

4.lower

返回字符串小写

5.replace将子串替换为另一个字符串,并放回替换后的结果

>>>"ZhangTianQi".replace("Zhang","Wang")

'WangTianQi'

6.split字符串拆分(与join相反)

>>> '1+2+3+4'.split('+')

['1', '2', '3', '4']

7.strip

strip删除开头和结尾的空白

strip(“字符串)删除原来字符串中开头和结尾指定的字符串(需要仔细研究)

 

 

你可能感兴趣的:(Python)