1.capitalize()方法
s = 'jack and tom and jack'
'''
capitalize() 方法:
Return a capitalized version of the string. ---> 返回字符串的大写版本
More specifically, make the first character have upper case and the rest lower case. ---> 更具体的说,使第一个字符具有大写字母,其余字母为小写字母
'''
2.title()方法
'''
title() 方法:
Return a version of the string where each word is titlecased.--> 返回一个字符串,其中每个单词的首字母大写
More specifically, words start with uppercased characters and all remaining
cased characters have lower case. --> 更具体的说,每个单词以大写字母开始,其余都是小写字母
'''
3.center()方法
'''
center() 方法:
Return a centered string of length width.-->返回长度为宽度的居中字符串。
Padding is done using the specified fill character (default is a space).-->填充是使用指定的填充字符完成的(默认为空格)
'''
print('abc'.center(9,'-'))
4.count()方法
'''
count() 方法:S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation.
查找子字符串 sub 在字符串中出现的次数,可选参数,开始 和 结束 可理解为切片
'''
print(s.count('and',1,3))
5.endswith()方法
'''
endswith() 方法:S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
如果S以指定后缀结尾,则返回True,否则返回False。
可以指定起始和结束位置,后缀也可以是要尝试的字符串元组。
'''
print('tom and jack'.endswith('jack',10))
6.find()方法
'''
find() 方法:S.find(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
在字符串 S 中查找子字符串sub,如果找到,返回子字符串sub在字符串s中首次出现的索引,没有找到返回 -1
参数start 和 end 可以理解为切片
'''
print('hello world python'.find('world',6))
print('hello world python'.find('world',6))
7.format()方法:
'''
format() 方法:S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
使用来自args和kwargs的替换返回S的格式化版本。替换用大括号(“{”和“}”)标识。
'''
s_format = '我的名字叫{},我的年龄是{}'.format('jack',19)
print(s_format)
s_format = '我的名字叫{name},我的年龄是{age},我的名字叫{name}'.format(name='jack',age=19)
print(s_format)
s_format = '我的名字叫{0},我的年龄是{1},我的名字叫{0}'.format('jack',19)
print(s_format)
8.index()方法
'''
index() 方法:S.index(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Raises ValueError when the substring is not found.
在字符串 s 中查找 子字符串 sub,在指定位置 start 和 end 之间查找,
参数 start 和 end 可理解为切片,如果没有找到,则会抛出 ValueError错误。找到,返回最先出现的索引
'''
s_index = 'return the lowest index in S where'
print(s_index.index('the'))
9.lower()方法:
'''
lower() 方法:
Return a copy of the string converted to lowercase.返回转换为小写的字符串副本。
将字符串中的所有字符转换成小写字母
'''
print('AbCdddEfgjKH'.lower())
10.lstrip()方法:
'''
lstrip() 方法:
Return a copy of the string with leading whitespace removed.
返回一个删除前导(左侧)空白字符的副本。
If chars is given and not None, remove characters in chars instead.
如果这个字符chars是给定的,而不是None,则删除指定的字符,而不是空白字符
'''
print(' abdde'.lstrip())
print('***abdcd'.lstrip('*'))
11.replace()方法:
'''
replace() 方法:
Return a copy with all occurrences of substring old replaced by new.
返回一个字符串的副本,使用新的子字符串替换旧的子字符串
count
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
替换的次数,默认为-1 ,表示替换所有的子字符串
If the optional argument count is given, only the first count occurrences are
replaced.
如果参数count给定的指定次数。则替换指定的次数
'''
print('abcabdabcabc'.replace('a','A',3))
12.split()方法:
'''
split()方法:
Return a list of the words in the string, using sep as the delimiter string.
将字符串使用指定分隔符进行拆分,并返回一个list
sep 用作拆分的分隔符
The delimiter according which to split the string.
None (the default value) means split according to any whitespace,
and discard empty strings from the result.
参数为空,默认使用空白字符进行拆分,返回的结果中抛弃空白字符
maxsplit 表示要执行的最大拆分
Maximum number of splits to do.
-1 (the default value) means no limit.
表示要执行的最大拆分次数,-1 默认值,表示没有限制
'''
print('Maximum number of splits to do'.split())
13.rstrip()方法
'''
rstrip() 方法:去掉字符串右边的字符,默认去掉空白字符,同 lstrip()方法
'''
print('abc***'.rstrip('*'))
'''
startswith() 方法: S.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try.
如果以指定的字符串开头,则返回True,否则返回False.
同endswith()方法类似
'''
print('abc'.startswith('a'))
14.strip()方法
'''
strip()方法: 同rstrip() 和 lstrip() 方法。去掉字符串首尾的空白字符,或者指定字符
'''
print('*%abc%'.strip('*%'))
15.upper()方法
'''
upper()方法:
Return a copy of the string converted to uppercase.
将字符串中的所有字符转换成大写字母,并返回一个字符串副本 同lower()方法对应
'''
print('abcdaAKJFA'.upper())