Basic operations of Python strings (very detailed)

1. Indexing and slicing of strings

Strings in the Python language include two serial number systems: forward ascending serial numbers and reverse descending serial numbers.

1.1 Index access of strings

1.2 String slice access 

The specific syntax format is: [head subscript: tail subscript], this access method is called "slicing". But note that this is an interval that is left closed and right open. In the slicing method, if the head subscript is defaulted, it means to get the substring from the beginning of the string; if the tail subscript is defaulted, it means to get the last character of the string; if both the head subscript and the tail subscript are default, then take the entire string. 

 String slicing can also set the order of substrings, the format is [head subscript: tail subscript: step size]. When the step size is greater than 0, characters are taken from left to right; when the step size is less than 0, characters are taken from right to left.

Second, the processing and operation of strings

2.1 Built-in string processing functions 

len(x)   #返回字符串x的长度
str(x)   #将任意类型的x转化为字符串类型
chr(x)   #返回Unicode编码为x的字符
ord(x)   #返回字符x的Unicode编码
hex(x)   #将整数x转化为十六进制数
oct(x)   #将整数x转化为八进制数

 

 find class function

find()   #查找一个字符串在另一个字符串指定范围内(默认是整个字符串)中首次出现的位置,若不存在则返回-1
rfind()  #查找一个字符串在另一个字符串指定范围内(默认是整个字符串)中最后一次出现的位置,若不存在则返回-1
index()  #查找一个字符串在另一个字符串指定范围内(默认是整个字符串)中首次出现的位置,若不存在则抛出异常
rindex() #查找一个字符串在另一个字符串指定范围内(默认是整个字符串)中最后一次出现的位置,若不存在则抛出异常
count()  #用来返回一个字符串在另一个字符串中出现的次数,若不存在则返回0

Split class function 

split()    #以指定字符为分隔符,从原字符串的左端开始将其分割为多个字符串,并返回包含分割结果的列表
rsplit()   #以指定字符为分隔符,从原字符串的右端开始将其分割为多个字符串,并返回包含分割结果的列表
partition()    #以指定字符串为分隔符将原字符串分割为3个部分,分隔符之前的字符串,分隔符字符串和分隔符之后的字符串
rpartition()   #以指定字符串为分隔符将原字符串分割为3个部分,分隔符之前的字符串,分隔符字符串和分隔符之后的字符串

 

String concatenation method 

join()    #将列表中多个字符串进行连接,并在相邻两个字符串之间插入指定字符,返回新字符串

 

Uppercase and lowercase character conversion method 

lower()     #将字符串转换为小写字符串
uppper()    #将字符串转换为大写字符串
capitalize()    #将字符串首字母变为大写
title()         #将字符串中每个单词的首字母都变为大写
swapcase()      #将字符串中的字符大小写互换

Note: These character conversion methods generate a new string without any modification to the original string. 

 Replacement method

replace()      #替换字符串中指定字符或子字符串

Delete consecutive blank characters and specified characters at both ends, right or left of a string 

strip()         #删除字符串两端空白字符
rstrip()        #删除字符串右端空白字符
lstrip()        #删除字符串左端空白字符

 

 Method to determine whether a string starts or ends with a specified string

startswith()   #判断字符串是否以指定字符开始
endswith()      #判断字符串是否以指定字符结束

 

Method for judging string type: 

isupper()       #是否全为大写
islower()       #是否全为小写
isdigit()       #是否全为数字
isalnum()       #是否全为字母或汉字或数字
isalpha()       #是否全为字母或汉字
>>> s = 'years'
>>> s.islower()
True
>>> s = 'YEARS'
>>> s.upper()
'YEARS'
>>> s.isupper()
True
>>> s = '20221015'
>>> s.isdigit()
True
>>> s = 'I am a girl'
>>> s.isalpha()
False
>>> s.isalpha()
False
>>> s = s.replace(' ','')      #需要将其中的空格删除
>>> s.isalpha()
True
>>> s.isalnum()
False

String typeset method 

center()         #字符串居中对齐
ljust()          #字符串居左对齐
rjust()          #字符串居右对齐
zfill()          #输出指定宽度,不足的左边填0

 

Three, format () formatting method

The format() method can have multiple output items, and the positions can be set in the specified order.

3.1 The default order and specified order of format()

When using the format() method to format a string, you first need to enter ":" in "{}", and then set <padding character> <alignment> <width> respectively after ":".

Use the format() method to set the number of reserved digits.

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_51769031/article/details/127322960