def capitalize(self):
str = 'aGe'
print(str.capitalize())
结果:Age
结论:首字母大写,其他字母都小写
def casefold(self):
str = 'AGE-age'
print(str.casefold())
结果:age-age
结论:首字母大写,其他字母都小写
def center(self, width, fillchar=None):
str = 'AGE-age'
print(str.center(20,'='))
结果:======AGE-age=======
结论:字符串str放在长为20的字符中,缺少的用fillchar字符来填充
def count(self, sub, start=None, end=None):
str = 'AGE-age'
print(str.count('ag'))
print(str.count('ag',5,6))
结果:1、0
结论:统计在字符串中出现特定字符的次数,也可以指定起始位置
def encode(self, encoding='utf-8', errors='strict'):
def endswith(self, suffix, start=None, end=None):
str = 'chian'
print(str.endswith('an',3,5))
结果:True
结论:返回是否以特定字符结尾的字符串,可以指定起始位置
def expandtabs(self, tabsize=None):
str = " alex"
print (str.expandtabs())
print(str.expandtabs(10))
结果: alex/ alex
结论:缺省的tab键是8个空格,加数字为数字位的空格
def find(self, sub, start=None, end=None):
str = "alex is a workhard man"
print (str.find('is'))
print (str.find('or',4,13))
结果:5/11
结论:在字符串中查找字符,可以指定起始字符
def format(*args, **kwargs):
def index(self, sub, start=None, end=None):
str = "alex is a workhard man"
print (str.index('is'))
结果:5
结论:在字符串中查找字符位置,可以指定起始字符
def isspace(self):
#-*- coding:UTF-8 -*-
str = ' fa'
print(str.isspace())
结果:False
结论:要求字符串都是空串
def istitle(self):
str = "This Is String Example...Wow!!!"
print (str.istitle())
str = "This is string example....wow!!!"
print (str.istitle())
结果:True/False
结论:只有首字母都是大写才是TITLE
def join(self, iterable):
str = "This Is String Example...Wow!!!"
print (':'.join(str))
str1 = ['This','is','a','book']
print (':'.join(str1))
str2 = ('This','is','a','book')
print (':'.join(str2))
str3 = {'1':'This','2':'is','3':'a','4':'book'}
print ('-'.join(str3))
结果:
T:h:i:s: :I:s: :S:t:r:i:n:g: :E:x:a:m:p:l:e:.:.:.:W:o:w:!:!:!
This:is:a:book
This:is:a:book
2-4-1-3
结论:用连接符把字符连接起来
def ljust(self, width, fillchar=None):
str1 = 'This is a book'
print (str1.ljust(20,'#'))
结果:This is a book######
结论:左对齐,默认填充字符为空
def maketrans(self, *args, **kwargs):
import string
str1 = 'abcde'
map = str1.maketrans('abcde','12345')
print (type(map))
print (map)
结果:
<class 'dict'>
{97: 49, 98: 50, 99: 51, 100: 52, 101: 53}
结论:ASCII a-97,1-49
def partition(self, sep):
import string
str = "http://www.w3cschool.cc/"
print (str.partition('//'))
结果:('http:', '//', 'www.w3cschool.cc/')
结论:返回三个字符串,左边是分隔符左边的,右边是分隔符右边的,中间是分割符
def replace(self, old, new, count=None):
str = "http://www.w3cschool.cc//"
print (str.replace('//','$$',1))
print (str.replace('//','$$',2))
结果:http:$$www.w3cschool.cc// http:$$www.w3cschool.cc$$
结论:用新字符替换这些旧的字符
def rfind(self, sub, start=None, end=None):
str = "hello,alex,alpha"
print (str.rfind('al',5,13))
结果:11
结论:找到最右边的这个字符,并且返回索引号
def splitlines(self, keepends=None):
str = "Line1-a b c d e f Line2- a b c Line4- a b c d";
print (str.splitlines( ))
print (str.splitlines( 0 ))
print (str.splitlines( 3 ))
print (str.splitlines( 4 ))
print (str.splitlines( 5 ))
结果:
['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d']
['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d']
['Line1-a b c d e f
', 'Line2- a b c
', '
', 'Line4- a b c d']
['Line1-a b c d e f
', 'Line2- a b c
', '
', 'Line4- a b c d']
['Line1-a b c d e f
', 'Line2- a b c
', '
', 'Line4- a b c d']
结论:返回一个包含各行作为元素的列表