欢迎来到Louis的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
扩大
缩小

Python 字符串操作

python2与python3中的字符串

在编程语言中,几乎90%的操作都是和字符串和整数在打交道,在python中字符串也是一种基础数据类型。

在早期的python2中,字符串与内置数据类型bytes是同一种东西,实际上python2中的字符串就是bytes。

而在python3中后,为了解决python2令人头疼的编码问题,python3的字符串就实际上是unicode字符串了,在python中有unicode的类型,在python中就取消unicode类型了,但实际上python3中的字符串就是unicode字符。

 

由于unicode中是包含中文,所以解释器能成功显示出中文,而python2中的默认编码是ascii码,默认是无法识别的,而python2中的字符串就是bytes,所以以二进制的方式显示。

 

字符串切片

在说切片前,要说一下索引这个东西,python有很多数据类型跟索引是息息相关的,字符串,列表,元组这些都有索引,我们可以使用索引对数据进行我们想要的操作。

我们来对一个str进行遍历:

s = "python"
for i,v in enumerate(s):
    print(i, v)
0 p
1 y
2 t
3 h
4 o
5 n

可以看出str是一个可迭代对象,我们使用enumerate配合for循环可以取到每个元素的索引和值。

在python中这种索引都是以0开始的,然后逐个递增。

我们来看几个切片的例子:

s = "python"
print(s[:3])  #起始为0,截取到3的位置,但不包含3
print(s[:-1])  #负数索引,-1表示最后一个,-2则是倒数第二个,以此类推
print(s[:])   #从头取到尾,在后面学到字典时,这种方式可以用于作为字典的复制。
print(s[::2])  #步长为2,逢2取1
print(s[::-1]) #步长为-1,可以理解为字符串的反转
print(s[-1:-3:-1]) #从右往左取
pyt
pytho
python
pto
nohtyp
no

我们可以看出字符串切片可以提供3个参数:

str[start:end:stemp]

start: 起始位置,不指定的话默认为0

end:结束位置,不指定默认到字符串结尾,但是指定了end的数值,切片获得的字符串是不包含end索引位置的字符的。

stemp:步长,可以理解为跳着取值,当步长为2时,则是逢2取一;当步长为正数时,或者不指定步长时,字符串从左往右进行切片,起始位置需要在结束位置的左边;如果步长为负数时,起始位置需要在结束位置的右边,而且返回的新字符串相当于做个反转。

  Note:可以这么理解,当我们步长为-1时,s[1:3:-1] 起始索引1 + 步长-1 =0,这时索引时0,是往左移了,然而设置的结束位置是在右边,这样索引是到不了结束位置的,所以这样切片是返回不了值的;我们看一下s[3:1:-1],这样情况下起始索引 3+ 步长-1= 2,新的索引是往结束位置靠近的,这样就可以界定整个切片的范围了,所以说,当索引为负数时,切片时从右往左进行的。

 

字符串方法:

1.capitalize()   把首字母变成大写

In [1]: s = "python"

In [2]: s.capitalize()
Out[2]: 'Python'

2.lower()    把所有字母变成小写

In [4]: s = "PyThoN"

In [5]: s.lower()
Out[5]: 'python'

In [6]:

3.upper()        把所有字母变成大写

In [4]: s = "PyThoN"

In [5]: s.upper()
Out[5]: 'PYTHON'

In [6]:

4.swapcase()  大小写互换

In [4]: s = "PyThoN"

In [5]: s.lower()
Out[5]: 'python'

In [6]:

5.title()    将字符串变成标题的格式(单词首字母大写,其他字母小写)

In [10]: s = "i love python"

In [11]: s.title()
Out[11]: 'I Love Python'

6.center(10,*)  把字符串居中,用指定字符填充两边

In [12]: s = "python"

In [13]: s.center(20,"*")
Out[13]: '*******python*******'

7.strip()    不指定参数,默认去除字符串左右两边的空白(\n,\t),指定参数则去除字串两边的指定字符

In [14]: s = """ python \n
    ...: """

In [15]: s.strip()
Out[15]: 'python'
In [16]: s = "***python****"

In [17]: s.strip("*")
Out[17]: 'python'

8.rstrip()  用法参考strip,区别是,rstrip只操作字串的右边

In [18]: s = "***python****"

In [19]: s.rstrip("*")
Out[19]: '***python'

9.lstrip()  用法参考strip,区别是,lstrip只操作字串的左边

In [22]: s = "***python****"

In [23]: s.lstrip("*")
Out[23]: 'python****'

10.replace(old,new,count=None)  替换字串中的指定字符串为新值,count为替换几次,默认全部替换

  s.replace(" ","") 去除字符串中所有的空格

In [24]: s = "i love python,i love everything!"

In [25]: s.replace("love","like")
Out[25]: 'i like python,i like everything!'
In [26]: s = "i love python,i love everything!"

In [27]: s.replace("love","like",1)
Out[27]: 'i like python,i love everything!'

11.split(sep=None,maxsplit(-1)) 切割字符串,默认使用空白进行切割,指定seq的话,则用seq进行切割,maxsplit默认切割所有,设置值的话,设置为几就切割几次

In [28]: s = "i love python,i love everything!"

In [29]: s.split()
Out[29]: ['i', 'love', 'python,i', 'love', 'everything!']
In [30]: s = "i love python,i love everything!"

In [31]: s.split("love")
Out[31]: ['i ', ' python,i ', ' everything!']
In [32]: s = "i love python,i love everything!"

In [33]: s.split("love",1)
Out[33]: ['i ', ' python,i love everything!']

12.format() 字符串格式化。

In [35]: "i love {one},i love {two}!".format(one="python",two="everything")
Out[35]: 'i love python,i love everything!'

13.startswith()  是否以指定字符串开头

In [37]: s.startswith("py")
Out[37]: True

In [38]: s.startswith("th")
Out[38]: False

14.endswith()  是否以指定字符串结尾

In [39]: s = "python"

In [40]: s.endswith("on")
Out[40]: True

In [41]: s.endswith("ho")
Out[41]: False

15.count()  计算指定字符在字符串中出现的次数

In [44]: s = "i love python,i love everything!"

In [45]: s.count("e")
Out[45]: 4

In [46]: s.count("e",0,10)
Out[46]: 1

16.find()  查找指定字符出现在字符串中第一次出现的位置,找不到返回-1

In [47]: s = "i love python,i love everything!"

In [48]: s.find("love")
Out[48]: 2

In [49]: s.find("like")
Out[49]: -1

17.index()  查找指定字符出现在字符串中第一次出现的位置,找不到会出现ValueError

In [50]: s = "i love python,i love everything!"

In [51]: s.index("love")
Out[51]: 2

In [52]: s.index("like")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-52-5c52c8570433> in <module>()
----> 1 s.index("like")

ValueError: substring not found

18.isdigit()  判断是否是数字

In [53]: n = "123"

In [54]: n.isdigit()
Out[54]: True

19.isalpha()  判断是否是字母

In [57]: s = "abc"

In [58]: s.isalpha()
Out[58]: True

20.isalnum() 判断是否是数字和字母组成

In [59]: s = "abc123"

In [60]: s.isalnum()
Out[60]: True

21.len()  计算字符串的长度

In [61]: s = "python"

In [62]: len(s)
Out[62]: 6

至此我们列举了很多字符串方法了,还有很多,如有右需要可自行查阅,https://docs.python.org/3/library/stdtypes.html?highlight=str#str

 

posted on 2018-07-30 18:43  Louiszj  阅读(176)  评论(0编辑  收藏  举报

导航