The basic string manipulation Python common set of functions

This article describes the common string operations Python basic set of functions, the need of a friend can refer
Here Insert Picture Description

1, the definition of the string

#定义空字符串<br>>>> name=''<br>#定义非空字符串
>>> name="luoahong"<br>#通过下标访问
>>> name[1]
'u'<br>#不能修改字符串的值,否则会报错
>>> name[2] = "3"
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Special note: If you modify a string, then the string where the memory address also followed changes, so can not be modified, but for the list, the list can be modified because the memory is constant, can be modified directly above

2, sliced

Special note: Actually strings and lists, tuples, also can slice

>>> name="ahong luo"
>>> name[1:4]  #取1到4之间的字符,包括1,不包括4
'hon'
>>> name[1:-1]  #取1到-1之间的字符,包括1,不包括-1
'hong lu'
>>> name[0:3] 
'aho'
>>> name[:3]   #从头开始取,0可以省略,效果等同于name[0:3]
'aho'
>>> name[3:]   #想取到最后一个值,必须不能写-1,只能这么写
'ng luo'
>>> name[0::2]  #后面的2表示:每隔一个字符就取一个
'aoglo'
>>> name[::2]   #从头开始0可以省略,效果跟上一句一样
'aoglo'

3, capitalized (capitalize ())

>>> name="ahong luo"
>>> name.capitalize()
'Ahong luo'
>>>

4、统计(count(sub,start=None,end=None))

sub: statistical character, start: start bit, end: the end of the bit, start and end default is empty, then all characters to count

>>> name="ahong luo"
#有开始和结束符
<br>
>>> name.count("o",1,5)
1<br>#从整个字符串中统计字符
>>> name.count("o")<br>2
>>>

5、center(width,fillchar)

If the width (character width) is smaller than or equal to (<=) the length of the string, the original string is returned, if greater than (>) string, filled with FillChar (fill character), the processing result is equal width, the string in the middle of fillchar

>>> name="ahong luo"
>>> len(name)
9<br>#10小于等于字符串宽度
>>> name.center(12,'-')
'-ahong luo--'<br>#50大于字符串长度,并且字符串位于填充字符的中间
>>> name.center(50,'-')
'--------------------ahong luo---------------------'
>>> len(name.center(50,'-'))
50

6, light (width, fillchar)

If the string is greater than the width (character width), returns the original string, is less than width, with the FillChar (fill character) filled, the processing result is equal width, right-most character string FillChar

>>> name="ahong luo"
>>> len(name)
9<br>#小于等于字符串的长度
>>> name.ljust(8,'-')
'ahong luo'<br>#大于字符串的长度
>>> name.ljust(50,'-')
'ahong luo-----------------------------------------'

Special note: ljust l is left in the meaning of representation from right to left

7.rjust(width,fillchar)

如果字符串的长度大于width(字符宽度),则返回原字符串,如果小于width,则用fillchar(填满字符)填满,处理结果等于width,fillchar位于字符串的最左边

>>> name="ahong luo"<br>>>> len(name)
9
>>> name.rjust(8,'-')
'ahong luo'
>>> name.rjust(50,'-')
'-----------------------------------------ahong luo' 

特别提醒:ljust中的l表示left的意思,表示从左向右

8、编码(encode)

字符串的编码和解码,在这边不用说了,我专门写了一篇博客,详细地址:猛击这里

9、endwith(suffix,start=None,end=None)

判断是否已suffix结尾,是返回True,否返回Fales

suffix:表示字符,start:开始位,end:结束位,start和end默认为空是,表示从整个字符串的结尾去判断>>> name=“ahong luo”

<em id="__mceDel">>>> name.endswith('0',1,4) False<br> >>> name.endswith('o') True >>> name.endswith('o',1,4) False</em>

10、find(sub,start=None,end=None)

全文查找sub中第一个字符所在整个字符串中的索引值,没有找到则返回-1

sub:字符或者字符串,start:开始位,end:结束位,start和end默认为空时,则在整个字符串中查找

>>> name="ahong luo"<br><br>#没找到,则返回-1
>>> name.find("lu",1,4)
-1
>>> name.find("a",2)
-1
>>> name.find("lu")
6
>>> name.find("lu",1,6)
-1
>>> name.find("lu",1,10)
6
>>>

推荐我们的Python学习扣qun:913066266 ,看看前辈们是如何学习的!从基础的python脚本到web开发、爬虫、django、数据挖掘等【PDF,实战源码】,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每天都有大牛定时讲解Python技术,分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地

11、rfind(sub,start=None,end=None)

从左向右查找sub中第一个字符所在整个字符串中的索引值,没有找到则返回-1

>>> name="ahong luo"<br>#找到
>>> name.rfind("a")
0<br>#未找到
>>> name.rfind("a",2)
-1

12、format()

①关键字拼接也是官方推荐
 
1
2
3
4
5
6
7
8
9
name = "alex"
age = 18
  
info = '''----info-----
name:{_name}
age:{_age}
 '''.format(_name=name,_age=age)
  
print(info)
 ②占位符拼接
 
1
2
3
4
5
6
8
7
9
name = "alex"
age = 18
  
info = '''----info-----
name:{0}
age:{1}
 '''.format(name,age)
  
print(info)

## 13、format_map()

数据格式,以字典形式传入

>>> name="name:{name},age:{age}"
>>> name.format_map({"name":"luoahong",'age':23})<br>#输出结果
'name:luoahong,age:23'

14、isalnum()

是否是一个阿拉伯数字和字母,它包含因为英文字符+(1-9)数字,中间不能有特殊字符

>>> age='23'
>>> age.isalnum()
True<br>#有特殊字符的
>>> age='ab23'
>>> age='ab'
>>> age.isalnum()
True
>>> age='ab'
>>> age.isalpha()
True
>>>

15、isalpha()

是否是一个纯的英文字符,包含大写

>>> age = 'ab'
>>> age.isalpha()
True
#夹着数字
>>> age = 'ab23'
>>> age.isalpha()
False
#大写
>>> age = 'Ab'
>>> age.isalpha()
True

16、isdigit()

判断是否是一个整数

17、isspace()

判断是否是一个空格

#不是空格
>>> age = ' age'
>>> age.isspace()
False
#是空格
>>> age = ' '
>>> age.isspace()
True

18、istitle()

是否是一个标题(字符串中的每个单子首字母大写)

#每个单词的首字母是小写<br>>>> name="luoahong"
>>> name.istitle()
False
>>> name="luo a hong"
>>> name.istitle()
False<br>#每个单词的首字母大写
>>> name="Luo A Hong"
>>> name.istitle()
True
>>> name="Luoahong"
>>> name.istitle()
True 

19、isupper()

是否是大写,注意字符串必须全部是大写才会返回True,否则返回False

#全部大写<br>>>> name="LUO"
>>> name.isupper()
True<br>#小写
>>> name="Luo"
>>> name.isupper()
False
>>>

## ```20、join()

序列用某个字符拼接成一个字符串,注意的是,序列的元素必须是str类

a = [‘1’,‘2’,‘3’]
‘+’.join(a)
‘1+2+3’

21、lower()


字符串中的大写字母转换为小写字母

name=“LuoAHong”
name.lower()
‘luoahong’


## 22、upper()

字符串中的小写字母转换为大写字母

name=“LuoAHong”
name.upper()
‘LUOAHONG’


## 23、strip()

去掉左右两边的空格(space)和回车(\n)

name= " \n luoahong \n"
name.strip()
‘luoahong’

 

## 24、lstrip()

删掉左边到右的空格(space)和回车(\n)

name= " \n luoahong \n"
name.lstrip()
'luoahong \n


## 25、rstrip()

删掉右边到左边的空格(space)和回车(\n)

name= " \n luoahong \n"
name.rstrip()
’ \n luoahong’

至此,方法前有l(left)和r(right)的函数,都是对之前函数的一个扩展,带l的表示至始至终从右到左操作,记住左边才是最终目的地,而带r的至始至终都是从左到右,因为右边才是它的终极目标

## 26、split()

分割函数,默认是以空格分割(space)生成一个列表,如果其他字符分割,输入其他字符参数

name=“ahong luo”
#默认为空,按空格分割
name.split()
[‘ahong’, ‘luo’]

name=“ahong+luo”
#以"+“字符分割
>>> name.split(”+")
[‘ahong’, ‘luo’]
#以’\n’分割

name=“ahong\nluo”
name.split("\n")
[‘ahong’, ‘luo’]


## 27、splitlines()

以换行符分割,这个一般在windows上开发,移到Linux上执行,或者在Linux上开发,移到Windows上执行,因为换行在windows上是"\r\n",linux上是'\n'

name=“ahong\nluo”
name.splitlines()
[‘ahong’, ‘luo’]


## 28、swapcase()

把大写换成小写,把小写换成大写

name=“Luo”
name.swapcase()
‘lUO’


## 29、startswith(prefix, start=None,end=None)

判断是否以prefix开始,是返回True,否返回Fales

prefix:表示字符或者字符或者字符串,start:开始位,end:结束位,start和end默认为空是,表示从整个字符串的结尾去判断

name=“ahong luo”
name.startswith(“luo”)  #开头找到"luo"字符串
False

name.startswith(“h”,3,5)  #在索引3到5之间没有找到以字符’h’开头
False


## 30、replace(old,new[, max])

old:将被替换的子字符串; new:新字符串,用于替换old子字符串;max:可选字符串, 替换不超过 max 次

name=“wo shi luo a hong”
name.replace(“shi”,“bushi”)
‘wo bushi luo a hong’
#原字符串没有改变

name.replace(“shi”,“bushi”,0)
‘wo shi luo a hong’

name.replace(“shi”,“bushi”,1)
'wo bushi luo a hong


## 31、zfill(width)

字符的长度是否大于等于(>=)with,如果比width小,则在字符串钱用0填充,如果>=width,则返回原字符串

name=“luoahong”
len(name)
8
#width大于字符串长度

name.zfill(11)
‘000luoahong’
#width小于等于字符串长度

name.zfill (8)
, luoahong '


Published 35 original articles · won praise 37 · views 30000 +

Guess you like

Origin blog.csdn.net/haoxun03/article/details/104255044
Recommended