自学一段时间之后,突然回首往昔,提笔忘字。

现在回头重新总结,以后还会不停更新总结。

1、字符串的创建

#coding=utf-8        这句话一定要写在第一行。第一行。第一行。
stringX = ""                   #空字符串
stringY = "    AbCdEfG    "    #有内容的字符串
stringZ = str(*)               #空字符串    *可以是数字,字符串,LIST,DICT,tuple,dict。
#coding=utf-8        这句话一定要写在第一行。第一行。第一行。
stringX = ""                   #空字符串
stringY = "    AbCdEfG    "    #有内容的字符串
stringZ = str(*)               #空字符串    *可以是数字,字符串,LIST,DICT,tuple,dict。

2、字符串的基本方法

#此例子只为说明"生成一个副本"的含义:生成一个新的对象在操作,不在原值(S)上操作。
S = "abcdef"
S.capitalize()
Z = S.capitalize()
print S                       #结果是abcdef
print S.capitalize()          #结果是Abcdef
print Z                       #结果是Abcdef
#此例子只为说明"生成一个副本"的含义:生成一个新的对象在操作,不在原值(S)上操作。
S = "abcdef"
S.capitalize()
Z = S.capitalize()
print S                       #结果是abcdef
print S.capitalize()          #结果是Abcdef
print Z                       #结果是Abcdef

2.1、字符串字母大小写操作 

S.capitalize()             #副本,首字母大写,返回值类型String。
S.lower()                  #副本,将S中内容全部转换成小写字母,返回值类型string
S.upper()                  #副本,将S中内容全部转换成大写字母,返回值类型string
S.swapcase()               #副本。将S中所有字母大小写互换。返回值类型string
S.title()                  #将S中所有符号之前的单词首字母变为大写。返回值类型string
#S.startswith(suffix[, start[, end]]) #判断S的第一位是否是suffix开头。返回值类型bool
#S.endswith(suffix[, start[, end]])   #判断S最后一位是否是suffix。返回值类型bool
S.capitalize()             #副本,首字母大写,返回值类型String。
S.lower()                  #副本,将S中内容全部转换成小写字母,返回值类型string
S.upper()                  #副本,将S中内容全部转换成大写字母,返回值类型string
S.swapcase()               #副本。将S中所有字母大小写互换。返回值类型string
S.title()                  #将S中所有符号之前的单词首字母变为大写。返回值类型string
#S.startswith(suffix[, start[, end]]) #判断S的第一位是否是suffix开头。返回值类型bool
#S.endswith(suffix[, start[, end]])   #判断S最后一位是否是suffix。返回值类型bool

 2.2、字符串拼接操作

#S.join(iterable)                       #字符串拼接方法之一,也是Python官方推荐的拼接方式
#↑以S为拼接符,以iterable为拼接内容。iterable可以为list、tuple、set等可迭代的数据结构,返回值类型string
#S.format(*args, **kwargs)              #字符串拼接方法之一
print "hello"+"world"                   #字符串拼接方法之最不推荐的一种方法,也是我最习惯的方法T.T
"my name is %s,I'm %d" % ("asktao",28) #字符串拼接方法之一
#S.join(iterable)                       #字符串拼接方法之一,也是Python官方推荐的拼接方式
#↑以S为拼接符,以iterable为拼接内容。iterable可以为list、tuple、set等可迭代的数据结构,返回值类型string
#S.format(*args, **kwargs)              #字符串拼接方法之一
print "hello"+"world"                   #字符串拼接方法之最不推荐的一种方法,也是我最习惯的方法T.T
"my name is %s,I'm %d" % ("asktao",28) #字符串拼接方法之一

 2.3、字符串定位、索引,查询操作

#返回值类型都是int
#S.count(sub[, start[, end]])      #返回sub在S中出现的次数,返回值类型int
#S.find(sub [,start [,end]])       #在S中从左搜索sub值,返回sub第一次出现的下标。如果无,则返回-1
#S.index(sub [,start [,end]])      #在S中搜索sub值,返回sub第一次出现的下标。如果无,则报异常
#S.rfind(sub [,start [,end]])      #在S中从右搜索sub值,返回sub第一次出现的下标。如果无,则返回-1
#S.rindex(sub [,start [,end]])     #在S中搜索sub值,返回sub第一次出现的下标。如果无,则报异常
#返回值类型都是int
#S.count(sub[, start[, end]])      #返回sub在S中出现的次数,返回值类型int
#S.find(sub [,start [,end]])       #在S中从左搜索sub值,返回sub第一次出现的下标。如果无,则返回-1
#S.index(sub [,start [,end]])      #在S中搜索sub值,返回sub第一次出现的下标。如果无,则报异常
#S.rfind(sub [,start [,end]])      #在S中从右搜索sub值,返回sub第一次出现的下标。如果无,则返回-1
#S.rindex(sub [,start [,end]])     #在S中搜索sub值,返回sub第一次出现的下标。如果无,则报异常

2.4、字符串补位与填充操作

#S.center(width[, fillchar])   #副本,S在左,两侧填充fillchar的内容,如果“无”则填充空格
#S.ljust(width[, fillchar])    #副本,S在左,右侧填充fillchar的内容,如果“无”则填充空格
#S.rjust(width[, fillchar])    #副本,S在右,左侧填充fillchar的内容,如果“无”则填充空格


#S.strip([chars])       #副本,删除S两侧指定字符chars,默认为空格,返回值类型string或unicode?
#S.lstrip([chars])      #副本,删除S左侧指定字符chars,默认为空格,返回值类型string或unicode?
#S.rstrip([chars])      #副本,删除S右侧指定字符chars,默认为空格,返回值类型string或unicode?

#S.zfill(width)         #副本,S向右对齐,左侧以0补位。返回指定长度的字符串。返回值类型string
#S.center(width[, fillchar])   #副本,S在左,两侧填充fillchar的内容,如果“无”则填充空格
#S.ljust(width[, fillchar])    #副本,S在左,右侧填充fillchar的内容,如果“无”则填充空格
#S.rjust(width[, fillchar])    #副本,S在右,左侧填充fillchar的内容,如果“无”则填充空格


#S.strip([chars])       #副本,删除S两侧指定字符chars,默认为空格,返回值类型string或unicode?
#S.lstrip([chars])      #副本,删除S左侧指定字符chars,默认为空格,返回值类型string或unicode?
#S.rstrip([chars])      #副本,删除S右侧指定字符chars,默认为空格,返回值类型string或unicode?

#S.zfill(width)         #副本,S向右对齐,左侧以0补位。返回指定长度的字符串。返回值类型string

2.5、字符串切片、拆分操作 

#S.split([sep [,maxsplit]])     #字符串拆分,在S中以sep为分隔符,拆分S。返回list。maxsplit为拆分次数
#S.rsplit([sep [,maxsplit]]     #字符串拆分,在S中以sep为分隔符,从右拆分S。返回list。
#S.splitlines(keepends=False)   #字符串S以行拆分,组成一个List,返回值类型list。keepends不懂—求解!


#S.partition(sep)       #在S中从左搜索sep,如果有sep,sep之前的内容为head,之后的内容为tail。
#如果S中没有sep索引值,则返回(head=S,"","")  返回值类型tuple(head, sep, tail)
#S.rpartition(sep)      #在S中从右搜索sep,如果有sep,sep之前的内容为head,之后的内容为tail。
#如果S中没有sep索引值,则返回("","",tail=S)  返回值类型tuple(head, sep, tail)

S = "1234567890"
S[1]     #内容"2"            #从零计数,输出在第二位的值。
S[1:]    #内容"234567890"    #输出从第二位到最后一位,等同于S[1:-1]
S[-1]    #内容"0"            #输出最后一位的值
S[1:6]   #内容"23456"        #输出从第二位开始到第六位之间但不包括第六位,也就是从第二位到第五位。
#S.split([sep [,maxsplit]])     #字符串拆分,在S中以sep为分隔符,拆分S。返回list。maxsplit为拆分次数
#S.rsplit([sep [,maxsplit]]     #字符串拆分,在S中以sep为分隔符,从右拆分S。返回list。
#S.splitlines(keepends=False)   #字符串S以行拆分,组成一个List,返回值类型list。keepends不懂—求解!


#S.partition(sep)       #在S中从左搜索sep,如果有sep,sep之前的内容为head,之后的内容为tail。
#如果S中没有sep索引值,则返回(head=S,"","")  返回值类型tuple(head, sep, tail)
#S.rpartition(sep)      #在S中从右搜索sep,如果有sep,sep之前的内容为head,之后的内容为tail。
#如果S中没有sep索引值,则返回("","",tail=S)  返回值类型tuple(head, sep, tail)

S = "1234567890"
S[1]     #内容"2"            #从零计数,输出在第二位的值。
S[1:]    #内容"234567890"    #输出从第二位到最后一位,等同于S[1:-1]
S[-1]    #内容"0"            #输出最后一位的值
S[1:6]   #内容"23456"        #输出从第二位开始到第六位之间但不包括第六位,也就是从第二位到第五位。

2.6、字符串判断操作

S.isalnum()          #判断S中所有元素内容是否为数字和字母。返回值类型bool
S.isalpha()          #判断S中所有元素内容是否为字母。返回值类型bool
S.isdigit()          #判断S中所有元素内容是否全部为数字。返回值类型bool
S.islower()          #判断S中的字母是否都为小写,返回值类型bool
S.isspace()          #判断S中是否内容全部为空格,返回值类型bool
S.istitle()          #判断S中是否在符号之后的第一位为大写,返回值类型bool
S.isupper()          #判断S中所有的字母是否为大写,返回值类型bool
S.isalnum()          #判断S中所有元素内容是否为数字和字母。返回值类型bool
S.isalpha()          #判断S中所有元素内容是否为字母。返回值类型bool
S.isdigit()          #判断S中所有元素内容是否全部为数字。返回值类型bool
S.islower()          #判断S中的字母是否都为小写,返回值类型bool
S.isspace()          #判断S中是否内容全部为空格,返回值类型bool
S.istitle()          #判断S中是否在符号之后的第一位为大写,返回值类型bool
S.isupper()          #判断S中所有的字母是否为大写,返回值类型bool

2.7、字符串替换操作

#S.replace(old, new[, count])       #副本,替换S中的指定字符。count默认值是-1.返回值类型string
#S.expandtabs([tabsize])            #指定转换字符串中的 tab 符号('\t')转为空格的字符

#S.translate(table [,deletechars]) 
#根据table,翻译S中的指定内容。翻译的内容长度必须一致。依赖maketrans。返回值类型string
#from string import maketrans
#S.replace(old, new[, count])       #副本,替换S中的指定字符。count默认值是-1.返回值类型string
#S.expandtabs([tabsize])            #指定转换字符串中的 tab 符号('\t')转为空格的字符

#S.translate(table [,deletechars]) 
#根据table,翻译S中的指定内容。翻译的内容长度必须一致。依赖maketrans。返回值类型string
#from string import maketrans

2.8、字符串编码操作

#S.decode([encoding[,errors]]) -> object
#S.encode([encoding[,errors]]) -> object 
#PS:下面分享了一个链接,详细内容请看链接。
#S.decode([encoding[,errors]]) -> object
#S.encode([encoding[,errors]]) -> object 
#PS:下面分享了一个链接,详细内容请看链接。

3、字符串的具体实现

3.1、字符串字母大小写操作 

#S.capitalize()               #副本,首字母大写,返回值类型String。
print "abcdef".capitalize()     #结果是Abcdef

#S.lower()                    #副本,将S中内容全部转换成小写字母,返回值类型string
print "asktao!890".lower()      #输出内容"asktao!890"
print "Asktao!890".lower()      #输出内容"asktao!890"
print "Asktao!890".lower()      #输出内容"asktao!890"

#S.upper()                    #副本,将S中内容全部转换成大写字母,返回值类型string
print "asktao!890".upper()      #输出内容"ASKTAO!890"
print "Asktao!890".upper()      #输出内容"ASKTAO!890"
print "Asktao!890".upper()      #输出内容"ASKTAO!890"

#S.swapcase()                 #副本。将S中所有字母大小写互换。返回值类型string
print "Asktao!890Z".swapcase()      #输出内容"aSKTAO!890z"

#S.title()                     #将S中所有符号之前的单词首字母变为大写。返回值类型string
print "my name is Asktao.i love programming!".title()
#输出内容"My Name Is Asktao.I Love Programming!"

#S.startswith(suffix[, start[, end]]) #判断S的第一位是否是suffix开头。返回值类型bool
print "asktao".startswith("a")           #输出内容 True
print "asktao".startswith("o",1,-2)      #输出内容 False
print "asktao".startswith(("z","a","q")) #输出内容True  #suffix也可以是一个元祖。


#S.endswith(suffix[, start[, end]]) #判断S最后一位是否是suffix。返回值类型bool
print "asktao".endswith("o")              #输出内容 True
print "asktao".endswith("o",0,-2)         #输出内容 False
print "asktao".endswith(("z","o","q"))    #输出内容True  #suffix也可以是一个元祖。
#S.capitalize()               #副本,首字母大写,返回值类型String。
print "abcdef".capitalize()     #结果是Abcdef

#S.lower()                    #副本,将S中内容全部转换成小写字母,返回值类型string
print "asktao!890".lower()      #输出内容"asktao!890"
print "Asktao!890".lower()      #输出内容"asktao!890"
print "Asktao!890".lower()      #输出内容"asktao!890"

#S.upper()                    #副本,将S中内容全部转换成大写字母,返回值类型string
print "asktao!890".upper()      #输出内容"ASKTAO!890"
print "Asktao!890".upper()      #输出内容"ASKTAO!890"
print "Asktao!890".upper()      #输出内容"ASKTAO!890"

#S.swapcase()                 #副本。将S中所有字母大小写互换。返回值类型string
print "Asktao!890Z".swapcase()      #输出内容"aSKTAO!890z"

#S.title()                     #将S中所有符号之前的单词首字母变为大写。返回值类型string
print "my name is Asktao.i love programming!".title()
#输出内容"My Name Is Asktao.I Love Programming!"

#S.startswith(suffix[, start[, end]]) #判断S的第一位是否是suffix开头。返回值类型bool
print "asktao".startswith("a")           #输出内容 True
print "asktao".startswith("o",1,-2)      #输出内容 False
print "asktao".startswith(("z","a","q")) #输出内容True  #suffix也可以是一个元祖。


#S.endswith(suffix[, start[, end]]) #判断S最后一位是否是suffix。返回值类型bool
print "asktao".endswith("o")              #输出内容 True
print "asktao".endswith("o",0,-2)         #输出内容 False
print "asktao".endswith(("z","o","q"))    #输出内容True  #suffix也可以是一个元祖。

3.2、字符串拼接操作

#S.join(iterable)       #字符串拼接方法之一,也是Python官方推荐的拼接方式,返回值类型string
#以S为拼接符,以iterable为拼接内容。iterable可以为list、tuple、set等可迭代的数据结构
print "".join(["a","s","k","t","a","o"])      #输出内容"asktao"         #list
print "".join(("a","s","k","t","a","o"))      #输出内容"asktao"         #tuple
print "!".join(["a","s","k","t","a","o"])     #输出内容"a!s!k!t!a!o"    #S为拼接符
print "".join(set("asktao"))                  #输出内容"askot"          #set
print "".join({"a":1,"b":2})                  #输出内容"ab"             #dict
#↑以S为拼接符,以iterable为拼接内容。iterable可以为list、tuple、set等可迭代的数据结构,返回值类型string

#S.format(*args, **kwargs)              #字符串拼接方法之一
print "hi!My name is {}.I'm learning {} now".format("Asktao","Python")
print "hi!My name is {1}.I'm learning {0} now".format("Python","Asktao")
print "hi!My name is {name}.I'm learning {language} now".format(name = "Asktao",language = "Python")
#以上3句输出内容都是"hi!My name is Asktao.I'm learning Python now"

"my name is %s,I'm %d" % ("asktao",28)  #字符串拼接方法之一

print "hello"+"world"                    #字符串拼接方法之最不推荐的一种方法
#S.join(iterable)       #字符串拼接方法之一,也是Python官方推荐的拼接方式,返回值类型string
#以S为拼接符,以iterable为拼接内容。iterable可以为list、tuple、set等可迭代的数据结构
print "".join(["a","s","k","t","a","o"])      #输出内容"asktao"         #list
print "".join(("a","s","k","t","a","o"))      #输出内容"asktao"         #tuple
print "!".join(["a","s","k","t","a","o"])     #输出内容"a!s!k!t!a!o"    #S为拼接符
print "".join(set("asktao"))                  #输出内容"askot"          #set
print "".join({"a":1,"b":2})                  #输出内容"ab"             #dict
#↑以S为拼接符,以iterable为拼接内容。iterable可以为list、tuple、set等可迭代的数据结构,返回值类型string

#S.format(*args, **kwargs)              #字符串拼接方法之一
print "hi!My name is {}.I'm learning {} now".format("Asktao","Python")
print "hi!My name is {1}.I'm learning {0} now".format("Python","Asktao")
print "hi!My name is {name}.I'm learning {language} now".format(name = "Asktao",language = "Python")
#以上3句输出内容都是"hi!My name is Asktao.I'm learning Python now"

"my name is %s,I'm %d" % ("asktao",28)  #字符串拼接方法之一

print "hello"+"world"                    #字符串拼接方法之最不推荐的一种方法

3.3、字符串定位、索引,查询操作

#返回值类型都是int
#S.count(sub[, start[, end]])  #副本,返回sub在S中出现的次数,返回值类型int
print "asktao".count("a")       #输出内容 2
print "asktao".count("a",1)     #输出内容 1 #从第2个位置开始向后索引
print "asktao".count("a",1,-2)  #输出内容 0 #从第2个位置开始向后索引到倒数第二位停止。

#S.find(sub [,start [,end]])   #在S中从左搜索sub值,返回sub第一次出现的下标。如果无,则返回-1
print "asktao".find("a")           #输出内容0   #在出现在第一位。(从0计数)
print "asktao".find("a",1)         #输出内容4   #从第2位开始向后查找"a",第2个"a"在S的第5位,则显示4
print "asktao".find("a",1,3)       #输出内容-1   #从第2位到第4位之间索引"a"

#S.index(sub [,start [,end]])  #在S中搜索sub值,返回sub第一次出现的下标。如果无,则报异常
print "asktao".index("a")           #输出内容0   #在出现在第一位。(从0计数)
print "asktao".index("a",1)         #输出内容4   #从第2位开始向后查找"a",第2个"a"在S的第5位,则显示4
#print "asktao".index("a",1,-2)     #输出内容ValueError: substring not found的异常

#S.rfind(sub [,start [,end]])  #在S中从右搜索sub值,返回sub第一次出现的下标。如果无,则返回-1
print "asktao".rfind("a")           #输出内容4    #从右侧开始第一次出现的位置。(从0计数)
print "asktao".rfind("a",1)         #输出内容4    #从第2位开始到最后一位,从右查找"a"
print "asktao".rfind("a",0,3)       #输出内容0    #从第一位到第四位之间,从右查找"a"
print "asktao".rfind("z")           #输出内容-1   #没有在S中找到指定sub,则返回-1

#S.rindex(sub [,start [,end]])  #在S中搜索sub值,返回sub第一次出现的下标。如果无,则报异常
print "asktao".rindex("a")           #输出内容4    #从右侧开始第一次出现的位置。(从0计数)
print "asktao".rindex("a",1)         #输出内容4    #从第2位开始到最后一位,从右查找"a"
#print "asktao".rindex("a",1,-2)     #输出内容ValueError: substring not found的异常
#print "asktao".rindex("z")          #输出内容ValueError: substring not found的异常
#返回值类型都是int
#S.count(sub[, start[, end]])  #副本,返回sub在S中出现的次数,返回值类型int
print "asktao".count("a")       #输出内容 2
print "asktao".count("a",1)     #输出内容 1 #从第2个位置开始向后索引
print "asktao".count("a",1,-2)  #输出内容 0 #从第2个位置开始向后索引到倒数第二位停止。

#S.find(sub [,start [,end]])   #在S中从左搜索sub值,返回sub第一次出现的下标。如果无,则返回-1
print "asktao".find("a")           #输出内容0   #在出现在第一位。(从0计数)
print "asktao".find("a",1)         #输出内容4   #从第2位开始向后查找"a",第2个"a"在S的第5位,则显示4
print "asktao".find("a",1,3)       #输出内容-1   #从第2位到第4位之间索引"a"

#S.index(sub [,start [,end]])  #在S中搜索sub值,返回sub第一次出现的下标。如果无,则报异常
print "asktao".index("a")           #输出内容0   #在出现在第一位。(从0计数)
print "asktao".index("a",1)         #输出内容4   #从第2位开始向后查找"a",第2个"a"在S的第5位,则显示4
#print "asktao".index("a",1,-2)     #输出内容ValueError: substring not found的异常

#S.rfind(sub [,start [,end]])  #在S中从右搜索sub值,返回sub第一次出现的下标。如果无,则返回-1
print "asktao".rfind("a")           #输出内容4    #从右侧开始第一次出现的位置。(从0计数)
print "asktao".rfind("a",1)         #输出内容4    #从第2位开始到最后一位,从右查找"a"
print "asktao".rfind("a",0,3)       #输出内容0    #从第一位到第四位之间,从右查找"a"
print "asktao".rfind("z")           #输出内容-1   #没有在S中找到指定sub,则返回-1

#S.rindex(sub [,start [,end]])  #在S中搜索sub值,返回sub第一次出现的下标。如果无,则报异常
print "asktao".rindex("a")           #输出内容4    #从右侧开始第一次出现的位置。(从0计数)
print "asktao".rindex("a",1)         #输出内容4    #从第2位开始到最后一位,从右查找"a"
#print "asktao".rindex("a",1,-2)     #输出内容ValueError: substring not found的异常
#print "asktao".rindex("z")          #输出内容ValueError: substring not found的异常

3.4、字符串补位与填充操作

#S.center(width[, fillchar])   #副本,S在左,两侧填充fillchar的内容,如果“无”则填充空格
print "asktao".center(10)        #输出内容"  asktao  " 补齐10位
print "YEAH".center(10,"*")      #输出内容“***YEAH***” 补齐10位

#S.ljust(width[, fillchar])    #副本,S在左,右侧填充fillchar的内容,如果“无”则填充空格
print "asktao".ljust(10)         #输出内容"asktao    " 补齐10位
print "YEAH".ljust(10,"*")       #输出内容"YEAH******" 补齐10位

#S.rjust(width[, fillchar])    #副本,S在右,左侧填充fillchar的内容,如果“无”则填充空格
print "asktao".rjust(10)         #输出内容"    asktao" 补齐10位
print "YEAH".rjust(10,"*")       #输出内容"******YEAH" 补齐10位

#S.strip([chars])              #副本,删除S两侧指定字符chars,默认为空格
print "  asktao  ".strip()         #输出内容"asktao"
print "**asktao**".strip("*")      #输出内容"asktao"
print "**ask*tao**".strip("*")     #输出内容"ask*tao"

#S.lstrip([chars])             #副本,删除S左侧指定字符chars,默认为空格
print "  asktao  ".lstrip()         #输出内容"asktao  "
print "**asktao**".lstrip("*")      #输出内容"asktao**"

#S.rstrip([chars])             #副本,删除S右侧指定字符chars,默认为空格
print "  asktao  ".rstrip()         #输出内容"  asktao"
print "**asktao**".rstrip("*")      #输出内容"**asktao"

#S.zfill(width) -> string   #副本,S向右对齐,左侧以0补位。返回指定长度的字符串
# #我能吐槽吗?这个函数具体用在什么地方呢?
print "asktao".zfill(10)        #输出内容"0000asktao"
#S.center(width[, fillchar])   #副本,S在左,两侧填充fillchar的内容,如果“无”则填充空格
print "asktao".center(10)        #输出内容"  asktao  " 补齐10位
print "YEAH".center(10,"*")      #输出内容“***YEAH***” 补齐10位

#S.ljust(width[, fillchar])    #副本,S在左,右侧填充fillchar的内容,如果“无”则填充空格
print "asktao".ljust(10)         #输出内容"asktao    " 补齐10位
print "YEAH".ljust(10,"*")       #输出内容"YEAH******" 补齐10位

#S.rjust(width[, fillchar])    #副本,S在右,左侧填充fillchar的内容,如果“无”则填充空格
print "asktao".rjust(10)         #输出内容"    asktao" 补齐10位
print "YEAH".rjust(10,"*")       #输出内容"******YEAH" 补齐10位

#S.strip([chars])              #副本,删除S两侧指定字符chars,默认为空格
print "  asktao  ".strip()         #输出内容"asktao"
print "**asktao**".strip("*")      #输出内容"asktao"
print "**ask*tao**".strip("*")     #输出内容"ask*tao"

#S.lstrip([chars])             #副本,删除S左侧指定字符chars,默认为空格
print "  asktao  ".lstrip()         #输出内容"asktao  "
print "**asktao**".lstrip("*")      #输出内容"asktao**"

#S.rstrip([chars])             #副本,删除S右侧指定字符chars,默认为空格
print "  asktao  ".rstrip()         #输出内容"  asktao"
print "**asktao**".rstrip("*")      #输出内容"**asktao"

#S.zfill(width) -> string   #副本,S向右对齐,左侧以0补位。返回指定长度的字符串
# #我能吐槽吗?这个函数具体用在什么地方呢?
print "asktao".zfill(10)        #输出内容"0000asktao"

3.5、字符串切片、拆分操作 

#S.split([sep [,maxsplit]])     #字符串拆分,在S中以sep为分隔符,拆分S。返回list。maxsplit为拆分次数
print "astkao".split()              #输出内容['astkao']
print "astkao".split("z")           #输出内容['astkao']
print "a!s!k!t!a!o".split("!")      #输出内容['a', 's', 'k', 't', 'a', 'o']
print "a!s!k!t!a!o".split("!",3)    #输出内容['a', 's', 'k', 't!a!o']

#S.rsplit([sep [,maxsplit]]     #字符串拆分,在S中以sep为分隔符,从右拆分S。返回list。
print "astkao".rsplit()             #输出内容['astkao']
print "astkao".rsplit("z")          #输出内容['astkao']
print "a!s!k!t!a!o".rsplit("!")     #输出内容['a', 's', 'k', 't', 'a', 'o']
print "a!s!k!t!a!o".rsplit("!",3)   #输出内容['a!s!k', 't', 'a', 'o']    #这里和split()有区别

#S.splitlines(keepends=False)   #字符串S以行拆分,组成一个List,返回值类型list。keepends不懂—求解!
print "asktao\nAsktao\nASKTAO".splitlines()      #输出内容['asktao', 'Asktao', 'ASKTAO']

#S.partition(sep)       #在S中从左搜索sep,如果有sep,sep之前的内容为head,之后的内容为tail。
#如果S中没有sep索引值,则返回(head=S,"","")  返回值类型tuple(head, sep, tail)
print "asktao".partition("a")       #输出内容('', 'a', 'sktao')
print "asktao".partition("t")       #输出内容('ask', 't', 'ao')
print "asktao".partition("o")       #输出内容('askta', 'o', '')
print "asktao".partition("b")       #输出内容('asktao', '', '')

#S.rpartition(sep)       #在S中从右搜索sep,如果有sep,sep之前的内容为head,之后的内容为tail。
#如果S中没有sep索引值,则返回("","",tail=S)  返回值类型tuple(head, sep, tail)
print "asktao".rpartition("a")       #输出内容('askt', 'a', 'o')
print "asktao".rpartition("t")       #输出内容('ask', 't', 'ao')
print "asktao".rpartition("o")       #输出内容('askta', 'o', '')
print "asktao".rpartition("b")       #输出内容('', '', 'asktao')

#S[start:end:step]    #start开始位置,end结束位置,step步数(间隔数)。默认1正着走,-1倒着走,2跳着走。
S = "1234567890"
S[1]     #内容"2"            #从零计数,输出在第二位的值。
S[1:]    #内容"234567890"    #输出从第二位到最后一位,等同于S[1:-1]
S[-1]    #内容"0"            #输出最后一位的值
S[1:6]   #内容"23456"        #输出从第二位开始到第六位之间但不包括第六位,也就是从第二位到第五位。
S[::-1]  #内容"0987654321"   #翻转字符串,倒序输出S。   #这个给个彩蛋吧
S[::2]   #内容"13579"        
S[1::2]  #内容"24680"        #感受一下~
#S.split([sep [,maxsplit]])     #字符串拆分,在S中以sep为分隔符,拆分S。返回list。maxsplit为拆分次数
print "astkao".split()              #输出内容['astkao']
print "astkao".split("z")           #输出内容['astkao']
print "a!s!k!t!a!o".split("!")      #输出内容['a', 's', 'k', 't', 'a', 'o']
print "a!s!k!t!a!o".split("!",3)    #输出内容['a', 's', 'k', 't!a!o']

#S.rsplit([sep [,maxsplit]]     #字符串拆分,在S中以sep为分隔符,从右拆分S。返回list。
print "astkao".rsplit()             #输出内容['astkao']
print "astkao".rsplit("z")          #输出内容['astkao']
print "a!s!k!t!a!o".rsplit("!")     #输出内容['a', 's', 'k', 't', 'a', 'o']
print "a!s!k!t!a!o".rsplit("!",3)   #输出内容['a!s!k', 't', 'a', 'o']    #这里和split()有区别

#S.splitlines(keepends=False)   #字符串S以行拆分,组成一个List,返回值类型list。keepends不懂—求解!
print "asktao\nAsktao\nASKTAO".splitlines()      #输出内容['asktao', 'Asktao', 'ASKTAO']

#S.partition(sep)       #在S中从左搜索sep,如果有sep,sep之前的内容为head,之后的内容为tail。
#如果S中没有sep索引值,则返回(head=S,"","")  返回值类型tuple(head, sep, tail)
print "asktao".partition("a")       #输出内容('', 'a', 'sktao')
print "asktao".partition("t")       #输出内容('ask', 't', 'ao')
print "asktao".partition("o")       #输出内容('askta', 'o', '')
print "asktao".partition("b")       #输出内容('asktao', '', '')

#S.rpartition(sep)       #在S中从右搜索sep,如果有sep,sep之前的内容为head,之后的内容为tail。
#如果S中没有sep索引值,则返回("","",tail=S)  返回值类型tuple(head, sep, tail)
print "asktao".rpartition("a")       #输出内容('askt', 'a', 'o')
print "asktao".rpartition("t")       #输出内容('ask', 't', 'ao')
print "asktao".rpartition("o")       #输出内容('askta', 'o', '')
print "asktao".rpartition("b")       #输出内容('', '', 'asktao')

#S[start:end:step]    #start开始位置,end结束位置,step步数(间隔数)。默认1正着走,-1倒着走,2跳着走。
S = "1234567890"
S[1]     #内容"2"            #从零计数,输出在第二位的值。
S[1:]    #内容"234567890"    #输出从第二位到最后一位,等同于S[1:-1]
S[-1]    #内容"0"            #输出最后一位的值
S[1:6]   #内容"23456"        #输出从第二位开始到第六位之间但不包括第六位,也就是从第二位到第五位。
S[::-1]  #内容"0987654321"   #翻转字符串,倒序输出S。   #这个给个彩蛋吧
S[::2]   #内容"13579"        
S[1::2]  #内容"24680"        #感受一下~

3.6、字符串判断操作

#S.isalnum()           #判断S中所有元素内容是否为数字和字母。返回值类型bool
print "123456".isalnum()            #输出内容True
print "ask456".isalnum()            #输出内容True
print "a!1234".isalnum()            #输出内容False

#S.isalpha()           #判断S中所有元素内容是否为字母。返回值类型bool
print "asktao".isalpha()            #输出内容True
print "asktao55".isalpha()          #输出内容False

#S.isdigit()           #判断S中所有元素内容是否全部为数字。返回值类型bool
print "123456".isdigit()            #输出内容True
print "ask456".isdigit()            #输出内容False

#S.islower()            #判断S中的字母是否都为小写,返回值类型bool
print "asktao!555".islower()        #输出内容True
print "Asktao!555".islower()        #输出内容False

#S.isspace()            #判断S中是否内容全部为空格,返回值类型bool
print "".isspace()                  #输出内容False
print " ".isspace()                 #输出内容True
print "  ".isspace()                #输出内容True
print "a ".isspace()                #输出内容False

#S.istitle()            #判断S中的每句话中,第一位是否为大写开头,返回值类型bool
print "Asktao".istitle()            #输出内容True
print "asktao".istitle()            #输出内容False
print "ASKTAO".istitle()            #输出内容False
print "Asktao!A".istitle()          #输出内容True
print "Asktao!a".istitle()          #输出内容False

#S.isupper()            #判断S中所有的字母是否为大写,返回值类型bool
print "asktao".isupper()            #输出内容False
print "Asktao".isupper()            #输出内容True
print "ASKTAO".isupper()            #输出内容True
print "ASKTAO!456".isupper()        #输出内容False
#S.isalnum()           #判断S中所有元素内容是否为数字和字母。返回值类型bool
print "123456".isalnum()            #输出内容True
print "ask456".isalnum()            #输出内容True
print "a!1234".isalnum()            #输出内容False

#S.isalpha()           #判断S中所有元素内容是否为字母。返回值类型bool
print "asktao".isalpha()            #输出内容True
print "asktao55".isalpha()          #输出内容False

#S.isdigit()           #判断S中所有元素内容是否全部为数字。返回值类型bool
print "123456".isdigit()            #输出内容True
print "ask456".isdigit()            #输出内容False

#S.islower()            #判断S中的字母是否都为小写,返回值类型bool
print "asktao!555".islower()        #输出内容True
print "Asktao!555".islower()        #输出内容False

#S.isspace()            #判断S中是否内容全部为空格,返回值类型bool
print "".isspace()                  #输出内容False
print " ".isspace()                 #输出内容True
print "  ".isspace()                #输出内容True
print "a ".isspace()                #输出内容False

#S.istitle()            #判断S中的每句话中,第一位是否为大写开头,返回值类型bool
print "Asktao".istitle()            #输出内容True
print "asktao".istitle()            #输出内容False
print "ASKTAO".istitle()            #输出内容False
print "Asktao!A".istitle()          #输出内容True
print "Asktao!a".istitle()          #输出内容False

#S.isupper()            #判断S中所有的字母是否为大写,返回值类型bool
print "asktao".isupper()            #输出内容False
print "Asktao".isupper()            #输出内容True
print "ASKTAO".isupper()            #输出内容True
print "ASKTAO!456".isupper()        #输出内容False

3.7、字符串替换操作

#S.replace(old, new[, count])       #副本,替换S中的指定字符。count默认值是-1.返回值类型string
print "asktao789a".replace("a","A")     #输出内容"AsktAo789A"
print "asktao789a".replace("a","A",0)   #输出内容"asktao789a"
print "asktao789a".replace("a","A",1)   #输出内容"Asktao789a"
print "asktao789a".replace("a","A",2)   #输出内容"AsktAo789a"

#S.expandtabs([tabsize])            #指定转换字符串中的 tab 符号('\t')转为空格的字符
print "hi!\tI'm Asktao"                 #输出内容"hi! I'm Asktao"   #1个空格
print "hi!\tI'm Asktao".expandtabs(8)   #输出内容"hi!     I'm Asktao"  #5个空格
print "hi!\tI'm Asktao".expandtabs(16)  #输出内容"hi!             I'm Asktao"#13个空格

#S.translate(table [,deletechars]) 
#根据table,翻译S中的指定内容。翻译的内容长度必须一致。依赖maketrans。返回值类型string
# deletechars,翻译之后,删除S中的指定内容。deletechars = "ma" 实际等于删除S中的所有"m"和"a"
# # #感觉比zfill还要复杂的鸡肋。为什么内置方法还要依赖呢。

from string import maketrans
intab,outtab= "i","I"
trantab = maketrans(intab,outtab)   #这里应该是翻译表。要求翻译的内容长度必须一致,否则报异常。
print "my name is Asktao.i love programming!".translate(trantab)
#输出内容"my name Is Asktao.I love programmIng!"    #翻译,将所有"i"替换为"I"
print "my name is Asktao.i love programming!".translate(trantab,"ma")
#输出内容"y ne Is Askto.I love progrIng!"   #翻译之后,删除S中所有"m","a"
#S.replace(old, new[, count])       #副本,替换S中的指定字符。count默认值是-1.返回值类型string
print "asktao789a".replace("a","A")     #输出内容"AsktAo789A"
print "asktao789a".replace("a","A",0)   #输出内容"asktao789a"
print "asktao789a".replace("a","A",1)   #输出内容"Asktao789a"
print "asktao789a".replace("a","A",2)   #输出内容"AsktAo789a"

#S.expandtabs([tabsize])            #指定转换字符串中的 tab 符号('\t')转为空格的字符
print "hi!\tI'm Asktao"                 #输出内容"hi! I'm Asktao"   #1个空格
print "hi!\tI'm Asktao".expandtabs(8)   #输出内容"hi!     I'm Asktao"  #5个空格
print "hi!\tI'm Asktao".expandtabs(16)  #输出内容"hi!             I'm Asktao"#13个空格

#S.translate(table [,deletechars]) 
#根据table,翻译S中的指定内容。翻译的内容长度必须一致。依赖maketrans。返回值类型string
# deletechars,翻译之后,删除S中的指定内容。deletechars = "ma" 实际等于删除S中的所有"m"和"a"
# # #感觉比zfill还要复杂的鸡肋。为什么内置方法还要依赖呢。

from string import maketrans
intab,outtab= "i","I"
trantab = maketrans(intab,outtab)   #这里应该是翻译表。要求翻译的内容长度必须一致,否则报异常。
print "my name is Asktao.i love programming!".translate(trantab)
#输出内容"my name Is Asktao.I love programmIng!"    #翻译,将所有"i"替换为"I"
print "my name is Asktao.i love programming!".translate(trantab,"ma")
#输出内容"y ne Is Askto.I love progrIng!"   #翻译之后,删除S中所有"m","a"

3.8、字符串操编码操作

#S.decode([encoding[,errors]]) -> object
#S.encode([encoding[,errors]]) -> object
#PS:下面分享了一个链接,详细内容请看链接。
#S.decode([encoding[,errors]]) -> object
#S.encode([encoding[,errors]]) -> object
#PS:下面分享了一个链接,详细内容请看链接。

Python 的字符串总结到这里完成了第一阶段。