3.5 strings (strings depth understanding of some common methods)
- 1. slicing string
- 2. Access a single character in a string
- 3. Modify the string of characters
- 4. The method of operation of a character string
- 1) capitalize () method
- 2) casefold () method
- 3) center (wodth) Method
- 4) count (sub [, start [, end]]) Method
- 5) encode(encoding='utf-8',errors='strict')方法
- 6) endswith (sub [, start [, end]]) Method
- 7) expandtabs ([tabsize = 8]) Method
- 8) find (sub [, start [, end]]) Method
- 9) index (sub [, start [, end]]) Method
- 10) isalnum () method
- 11) isalpha () method
- 12) isdecimal () method
- 13) isdigit () method
- 14) islower () method
- 15) isnumeric () method
- 16) isspace () method
- 17) istitle () method
- 18) isupper () method
- 19) join (sub) method
- 20) ljust (width) method
- 21) lower () method
- 22) lstrip () method
- 23) partition (sub) Method
- 24)replace(old,new[,count])方法
- 25) rfind (sub [, start [, end]]) Method
- 26) rindex (sub [, start [, end]]) Method
- 27) rjust (width) Method
- 28) rpartition (sub) Method
- 29) rstrip () method deletes the end of the string of blanks
- 30) split (sep = None, maxsplit = -1) Method
- 31) splitlines (([keepends])) Method
- 32)startswith(prefix[,start[,end]])方法
- 33) strip ([chars]) Method
- 34) swapcase () method
- 35) title () method
- 36) translate (table) Method
- 37) upper () method
- 38) zfill (width) Method
1. slicing string
Fragment Silce operation list may be a string of operations
str1 = "I love weivid"
name1 = str1[:6]
print(name1) # 打印出前6 个字符
2. Access a single character in a string
Unlike other due python language character with a single character (char) type, when the access string, a method of providing python
single character only seen as a character string
by way of index index
print(str1)
char = str1[5] #通过索引访问字符串中 单个子字符
print(char)
3. Modify the string of characters
The intermediate string is inserted, the splicing operation
str2 = str1[:6] + "insert string" + str1[6:] #拼接
print(str2)
4. The method of operation of a character string
Here are some commonly used methods listed
String Methods | effect |
---|---|
capitalize() | The first character of the string to uppercase |
casefold() | All the characters in the entire string to lowercase |
center(width) | String center, and padded with spaces to the width of the new string length |
count(sub[, start[, end]]) | Returns the number of sub occurring inside the string, start and end parameters indicates a range, optional. |
encode(encoding=‘utf-8’, errors=‘strict’) | To encoding specified encoding format for encoding strings. |
endswith(sub[, start[, end]]) | Check whether the string sub substring end, a return True, otherwise False. parameter indicates the start and end range, optional. |
expandtabs([tabsize=8]) | The tab character string (\ t) are converted to spaces, if not specified, the default is the number of spaces tabsize = 8. |
find(sub[, start[, end]]) | Detecting whether the sub contained in the string, if the index value is returned, otherwise -1, start and end parameters indicates a range, optional. |
index(sub[, start[, end]]) | Like find methods, but not if the sub string will produce an exception. |
isalnum () | If there is at least one character string and all the characters are letters or numbers returns True, otherwise False. |
Islf () | If there is at least one character string and all the characters are the letters return True, otherwise False. |
isdecimal () | If the string contains only decimal digits returns True, otherwise False. |
isdigit() | If the string contains only digit returns True, otherwise False. |
islower() | These characters If the string contains at least one of alphanumeric characters, and all lowercase, True is returned, otherwise it returns False. |
isnumeric() | If the string contains only numeric characters, it returns True, otherwise False. |
isspace() | If the string contains only spaces, returns True, otherwise False. |
istitl A () | If the string is the title (all the words are in uppercase began rest of the letters are in lower case), then returns True, otherwise False. |
isupper() | If the string contains at least one case sensitive characters, and these characters are uppercase, True is returned, otherwise it returns False. |
join(sub) | String as a separator interposed between all of the characters in the sub. |
light (width) | Returns a string left-aligned and padded with spaces to the width of the length of the new string. |
lower() | Convert a string to all uppercase characters to lowercase. |
lstrip() | Remove all of the strings of spaces left |
partition(sub) | Found sub substring, the string into a 3-tuple (pre_sub, sub, fol_sub), if the string is not included in the sub returns ( 'original string', '', '') |
replace(old, new[, count]) | Replace the old sub-string to string new substring, if the count is specified, alternatively no more than count times. |
rfind(sub[, start[, end]]) | Similar to the find () method, but is beginning to find the right. |
rindex(sub[, start[, end]]) | Similar to the index () method, but starting from the right. |
rjust(width) | Returns a string right justified and padded with spaces to the width of the length of the new string. |
rpartition (sub) | Similar to the partition () method, but is beginning to find the right. |
rstrip() | Remove spaces at the end of the string. |
split(sep=None, maxsplit=-1) | The list is not the default parameters spliced substring after the separator to space string sections, if maxsplit parameters set, only the partition maxsplit substrings, returns slice. |
split lines (([notch ends])) | Whether to remove the line breaks in the output, the default is False, it does not contain line breaks; If True, the reserved line breaks. . |
startswith(prefix[, start[, end]]) | Check whether the string starts with prefix, it returns True, otherwise False. parameters may specify start and end range check, optional. |
strip([chars]) | Delete all strings front and back spaces, chars parameter can be customized character deleted, optional. |
swapcase() | Flip case string. |
title() | Returns the title (all the words are in uppercase start, the rest of the letters are lowercase) string. |
translate(table) | The rules table (may be made str.maketrans ( 'a', customized 'b')) of the character string conversion. |
upper() | Conversion string all lowercase characters to uppercase. |
zfill(width) | Returns a string of length to width, the original string is right-justified with zero-padded front. |
A brief note that some places use each of the following methods
1) capitalize () method
The first character of the string to uppercase
str1 = "xiaoxie"
str1_1 = str1.capitalize() #开头大写,返回一个新的字符串,可以被赋值
print(str1_1)
2) casefold () method
All the characters in the entire string to lowercase
str2 = "SEsfdsSdss"
str2.casefold() #将所有字符串大写改为小写,
#注意返回新的字符串,并没有改变 源字符串
print(str2)
str2_2 = str2.casefold()
print(str2_2)
3) center (wodth) Method
String center, and padded with spaces to the width of the new string length
str2.center(40) #将字符串str2 居中,40为填充的长度
str2_3 = str2.center(40)
print(str2_3)
4) count (sub [, start [, end]]) Method
Returns the number of sub occurring inside the string, start and end parameters indicates a range, optional
str3 ="I love weivid xixi"
print(str3)
str3_1 = str3.count('xi') #字符串'xi' 在字符串中出现的次数
print(str3_1) #打印‘xi’出现2次
5) encode(encoding=‘utf-8’,errors=‘strict’)方法
以encoding 指定的编码格式对字符串进行编码
#编码,后续提到
6) endswith(sub[,start[,end]])方法
检查字符串是否以sub 子字符串结束,如果时返回True,否则返回False. start和end 参数表示范围,可选
str4 ="I love weivid xixi"
print(str4)
str4_1 = str4.endswith('xi') #检查字符串是否以'xi'
print(str4_1) # 结果返回为True
7) expandtabs([tabsize=8])方法
把字符串中的tab符号 \t 转换为空格,如不指定参数,默认的空格数为tabsize = 8
str5 = "I\tlove\tweivid wangwei"
print(str5) #注意打印的结果,I后有七个空格,love后有四个空格,weivid后有两个
str5_1 = str5.expandtabs() #将字符串中的\t替换为空格,默认为8个空格
8) find(sub[,start[,end]])方法
检测sub是否包含在字符串中,如果有则返回索引值,否则返回-1,start和end参数表示范围,可选
str6 = "I love weivid xixi"
print(str6)
str6_1 = str6.find('efc')
print(str6_1) # 打印—1, 表示字符串里没有efc
str6_2 = str6.find('wei')
print(str6_2) # 打印 7, 表示从字符串的第八位开始有wei
9)index(sub[,start[,end]])方法
跟find 方法一样,不过如果sub不在字符串中会产生一个异常
str6 = "I love weivid xixi"
print(str6)
#str6_1 = str6.index('efc')
#print(str6_1) # 异常
str6_2 = str6.find('wei')
print(str6_2) # 打印 7, 表示从字符串的第八位开始有wei
find 相较于index来说,比较和谐,一般来说不愿意看到异常
10)isalnum()方法
如果字符串至少有一个字符并且所有字符都是字母或数字,则返回True,否则返回False
11)isalpha()方法
如果字符串至少有一个字符并且所有字符都是字母,则返回True,否则返回False
12)isdecimal()方法
如果字符串只包含十进制数字,则返回True,否则返回False
13)isdigit()方法
如果字符串只包含数字,则返回False
14)islower()方法
如果字符串中至少包含一个区分大小写的字符,并且这些字符都是小写,则返回True,否则返回False
如果字符串中是中文,则返回False
str7 = "陈磊"
str7_1 = str7.islower() #中文返回False
print(str7_1)
15)isnumeric()方法
如果字符串中只包含数字字符,则返回True,否则返回False
16)isspace()方法
如果字符串中只包含看空格,则返回True,否则返回False
17)istitle()方法
如果字符串是标题化(所有的单词都是大写开始,其余都是小写),则返回True,否则返回False
str8 = "Weivid"
str8_1 = str8.istitle() #返回True
print(str8_1)
str8 = "WeiviD"
str8_1 = str8.istitle() #返回False
print(str8_1)
18)isupper()方法
如果字符串至少包含一个区分大小写的字符并且这些字符都是大写,则返回True,否则返回False
19)join(sub)方法
以字符串作为分隔符,并使用空格填充所有的字符之间
str9 = "Weivid"
str9_1 = str9.join('123456') #字符串被原始的字符串隔开
print(str9_1)
20)ljust(width)方法
返回一个左对齐的字符串,并使用空格填充至长度为width的新字符串
21)lower()方法
转换字符串中所有大写字符为小写
22)lstrip()方法
去掉字符串左边的所有空格
str10 = " I love Weivid"
str10_1 = str10.lstrip() #去掉左边所有空格
print(str10_1)
23)partition(sub)方法
找到子字符串sub,把字符串分成一个3元组(pre_sub,sub,fol_sub),如果字符串中不包含sub则返回(‘原字符串’,’’,’’)
str11 = "I love Weivid"
str11_1 = str11.partition('ve') #
print(str11_1)
24)replace(old,new[,count])方法
把字符串中的old替换成new子字符串,如果count指定,则替换不超过count次
str12 = "I love Weivid ve"
str12_1 = str12.replace('ve', 'wang') #将所有的ve替换为wang
print(str12_1)
25)rfind(sub[,start[,end]])方法
类似与find()函数,是从右边开始
#对于很长的字符串,比如网页的信息,可以看作为一个很长的字符串,有时候需要从左或者是往右进行查看字符串,效率是不一样的
26)rindex(sub[,start[,end]])方法
类似于Index()函数,是从右边开始
27)rjust(width)方法
返回一个右对齐的字符串,并使用空格填充至长度为width的新字符串
28)rpartition(sub)方法
类似于partition()函数,是从右边开始
29)rstrip()方法 删除字符串末尾的空格
30)split(sep=None,maxsplit=-1)方法
不带参数默认是以空格分隔符切片字符串,如果maxsplit参数有设置,
则仅分割mxsplit个字符串,返回切片后的子字符串拼接的列表
str13 = "I love Weivid"
str13_1 = str13.split() #默认是以空格,就会切片,形成一个列表
print(str13_1)
str13_2 = str13.split('ve') # 以‘ve’作为分隔符,切片形成列表
print(str13_2)
31)splitlines(([keepends]))方法
According to '\ n' separator, a return line contains the elements as a list, if keepends parameter is specified before return line keepends
32)startswith(prefix[,start[,end]])方法
Check whether the string starts with prefix, it returns True, otherwise it returns False, start and end parameter to specify the scope of inspection, optional
33) strip ([chars]) Method
Delete all strings front and back spaces, chars parameter can be customized to delete characters, optional
intermediate space does not delete the string of #
str14 = " I love Weivid "
str14_1 = str14.strip() #
print(str14_1)
str14 = "ssssaaaasssss"
str14_2 = str14.strip('s') #删除首位以s开头或者结尾的字符串
print(str14_2)
34) swapcase () method
Flip-sensitive string
35) title () method
Returns the title (all the words are in uppercase beginning, the rest lowercase) string
36) translate (table) Method
The rules table (may be made str.maketrans ( 'a', 'b') custom) characters in a string conversion
str15 = "ssssaaaassss"
str15_1 = str15.translate(str.maketrans('s','b'))
print(str15_1) #按照table的转换规则,将所有的s替换为b
print(str.maketrans('s','b')) #返回assic码,{115:98},115,98分别为s 和 b 的assic码
37) upper () method
All lowercase characters converted to uppercase string
38) zfill (width) Method
Returns a string of length to width, the original string is right-justified with zero-padded front