内置函数str()字符串

class str(object):
"""
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.

从给定对象创建一个新字符串对象。如果编码或
指定了错误,则对象必须公开数据缓冲区。
将使用给定的编码和错误处理程序进行解码。
否则,返回对象的结果。__str__()(如果定义)
代表(或对象)。
默认getdefaultencoding()编码系统。
错误默认为“严格”。
"""

def capitalize(self): # real signature unknown; restored from __doc__
"""
S.capitalize() -> str

Return a capitalized version of S, i.e. make the first character
have upper case and the rest lower case.
Python capitalize() 将字符串的第一个字母变成大写,其他字母变小写。对于 8 位字节编码需要根据本地环境。
返回大写字母的s,也就是第一个字符。
有大写和小写小写。

实例
# >>>s = 'a, B'
# >>> s.capitalize()
# 'A, b'
#
# >>> s = ' a, B' # a 前面有空格
# >>> s.capitalize()
# ' a, b'
#
# >>> s = 'a, BCD'
# >>> s.capitalize()
# 'A, bcd'
"""
return ""

def casefold(self): # real signature unknown; restored from __doc__
"""
S.casefold() -> str

Return a version of S suitable for caseless comparisons.
返回一个版本适用于无壳的比较。
Python casefold() 方法是Python3.3版本之后引入的,其效果和 lower() 方法非常相似,都可以转换字符串中所有大写字符为小写。
两者的区别是:lower() 方法只对ASCII编码,也就是‘A-Z’有效,对于其他语言(非汉语或英文)中把大写转换为小写的情况只能用 casefold() 方法。

实例
S1 = "Runoob EXAMPLE....WOW!!!" #英文
S2 = "ß" #德语

print( S1.lower() )
print( S1.casefold() )
print( S2.lower() )
print( S2.casefold() ) #德语的"ß"正确的小写是"ss"
"""
return ""

def center(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""
S.center(width[, fillchar]) -> str

Return S centered in a string of length width. Padding is
done using the specified fill character (default is a space)
以长度宽度字符串为中心的返回s。填充的是
使用指定的填充字符完成(默认为空格)

描述
Python center() 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格。

参数
width --字符串的总宽度
fillchar --填充字符

返回值
该方法返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。

实例
# 以下实例展示了center()方法的实例:
# >>>str = 'runoob'
# >>> str.center(20, '*')
# '*******runoob*******'
# >>> str.center(20)
# ' runoob
"""
return ""

def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
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.
返回子串子在非重叠出现的次数
字符串s [开始:结束]。可选参数起始和结束是
按切片符号解释。

描述
Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
语法
count()方法语法:
str.count(sub, start= 0,end=len(string))
参数
sub -- 搜索的子字符串
start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
返回值
该方法返回子字符串在字符串中出现的次数。

实例
str = "this is string example....wow!!!";
sub = "i";
print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40)
sub = "wow";
print "str.count(sub) : ", str.count(sub)
"""
return 0

def encode(self, encoding='utf-8', errors='strict'): # real signature unknown; restored from __doc__
"""
S.encode(encoding='utf-8', errors='strict') -> bytes

Encode S using the codec registered for encoding. Default encoding
is 'utf-8'. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that can handle UnicodeEncodeErrors.

描述
Python encode()方法以 encoding指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。
语法
encode()方法语法:
str.encode(encoding='UTF-8',errors='strict')
参数
encoding -- 要使用的编码,如"UTF-8"。
errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。
返回值
该方法返回编码后的字符串。

实例
以下实例展示了encode()方法的实例:
#!/usr/bin/python
str = "this is string example....wow!!!";
print "Encoded String: " + str.encode('base64','strict')
"""
return b""

def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
"""
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.

描述
Python endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,
否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。

语法
endswith()方法语法:
str.endswith(suffix[, start[, end]])

参数
suffix -- 该参数可以是一个字符串或者是一个元素。
start -- 字符串中的开始位置。
end -- 字符中结束位置。

返回值
如果字符串含有指定的后缀返回True,否则返回False。

实例
以下实例展示了endswith()方法的实例:
#!/usr/bin/python

str = "this is string example....wow!!!";

suffix = "wow!!!";
print str.endswith(suffix);
print str.endswith(suffix,20);

suffix = "is";
print str.endswith(suffix, 2, 4);
print str.endswith(suffix, 2, 6);
"""
return False

def expandtabs(self, tabsize=8): # real signature unknown; restored from __doc__
"""
S.expandtabs(tabsize=8) -> str

Return a copy of S where all tab characters are expanded using spaces.
If tabsize is not given, a tab size of 8 characters is assumed.

描述
Python expandtabs() 方法把字符串中的 tab 符号(' ')转为空格,tab 符号(' ')默认的空格数是 8。

语法
expandtabs()方法语法:
str.expandtabs(tabsize=8)

参数
tabsize -- 指定转换字符串中的 tab 符号(' ')转为空格的字符数。

返回值
该方法返回字符串中的 tab 符号(' ')转为空格后生成的新字符串。

实例
以下实例展示了expandtabs()方法的实例:
#!/usr/bin/python
str = "this is string example....wow!!!";
print "Original string: " + str;
print "Defualt exapanded tab: " + str.expandtabs();
print "Double exapanded tab: " + str.expandtabs(1s6);
"""
return ""

def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
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.

Return -1 on failure.

描述
Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束)
范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

语法
find()方法语法:
str.find(str, beg=0, end=len(string))

返回值
如果包含子字符串返回开始的索引值,否则返回-1。

实例
以下实例展示了find()方法的实例:
实例(Python 3.0+)
#!/usr/bin/python3

str1 = "Runoob example....wow!!!"
str2 = "exam";

print (str1.find(str2))
print (str1.find(str2, 5))
print (str1.find(str2, 10))

# >>>info = 'abca'
# >>> print(info.find('a')) # 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0
# 0
# >>> print(info.find('a', 1)) # 从下标1开始,查找在字符串里第一个出现的子串:返回结果3
# 3
# >>> print(info.find('3')) # 查找不到返回-1
# -1

"""
return 0

def format(self, *args, **kwargs): # known special case of str.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 '}').

新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。
基本语法是通过 {} 和 : 来代替以前的 % 。
format 函数可以接受不限个参数,位置可以不按顺序。

实例
# >>>"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序
# 'hello world'
#
# >>> "{0} {1}".format("hello", "world") # 设置指定位置
# 'hello world'
#
# >>> "{1} {0} {1}".format("hello", "world") # 设置指定位置

"""
pass

def format_map(self, mapping): # real signature unknown; restored from __doc__
"""
S.format_map(mapping) -> str

Return a formatted version of S, using substitutions from mapping.
The substitutions are identified by braces ('{' and '}').





"""
return ""

def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.index(sub[, start[, end]]) -> int

Like S.find() but raise ValueError when the substring is not found.

描述
Python index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,
则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

语法
index()方法语法:
str.index(str, beg=0, end=len(string))

参数
str -- 指定检索的字符串
beg -- 开始索引,默认为0。
end -- 结束索引,默认为字符串的长度。

返回值
如果包含子字符串返回开始的索引值,否则抛出异常。

实例
以下实例展示了index()方法的实例:
#!/usr/bin/python

str1 = "this is string example....wow!!!";
str2 = "exam";

print str1.index(str2);
print str1.index(str2, 10);
print str1.index(str2, 40);

"""
return 0

def isalnum(self): # real signature unknown; restored from __doc__
"""
S.isalnum() -> bool

Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.

描述
Python isalnum() 方法检测字符串是否由字母和数字组成。

语法
isalnum()方法语法:
str.isalnum()

返回值
如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False

实例
以下实例展示了isalnum()方法的实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

str = "this2009"; # 字符中没有空格
print str.isalnum();

str = "this is string example....wow!!!";
print str.isalnum();
"""
return False

def isalpha(self): # real signature unknown; restored from __doc__
"""
S.isalpha() -> bool

Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.

描述
Python isalpha() 方法检测字符串是否只由字母组成。

语法
isalpha()方法语法:
str.isalpha()

返回值
如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False

实例
以下实例展示了isalpha()方法的实例:
#!/usr/bin/python3

str = "runoob"
print (str.isalpha())

str = "Runoob example....wow!!!"
print (str.isalpha())

"""
return False

def isdecimal(self): # real signature unknown; restored from __doc__
"""
S.isdecimal() -> bool

Return True if there are only decimal characters in S,
False otherwise.

描述
Python isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。
注意:定义一个十进制字符串,只需要在字符串前添加 'u' 前缀即可。

语法
isdecimal()方法语法:
str.isdecimal()

返回值
如果字符串是否只包含十进制字符返回True,否则返回False。

实例
以下实例展示了 isdecimal()函数的使用方法:
#!/usr/bin/python
str = u"this2009";
print str.isdecimal();

str = u"23443434";
print str.isdecimal();

"""
return False

def isdigit(self): # real signature unknown; restored from __doc__
"""
S.isdigit() -> bool

Return True if all characters in S are digits
and there is at least one character in S, False otherwise.

描述
Python isdigit() 方法检测字符串是否只由数字组成。

语法
isdigit()方法语法:
str.isdigit()

返回值
如果字符串只包含数字则返回 True 否则返回 False。

实例
以下实例展示了isdigit()方法的实例:
#!/usr/bin/python3

str = "123456";
print (str.isdigit())

str = "Runoob example....wow!!!"
print (str.isdigit())
"""
return False

def isidentifier(self): # real signature unknown; restored from __doc__
"""
S.isidentifier() -> bool

Return True if S is a valid identifier according
to the language definition.

Use keyword.iskeyword() to test for reserved identifiers
such as "def" and "class".

描述
判断字符串是否是合法的标识符,字符串仅包含中文字符合法,实际上这里判断的是变量名是否合法。

实例
‘_a'.isidentifier() -->True
‘3a'.isidentifier() -->False
‘中国'.isidentifier() -->True

"""
return False

def islower(self): # real signature unknown; restored from __doc__
"""
S.islower() -> bool

Return True if all cased characters in S are lowercase and there is
at least one cased character in S, False otherwise.

描述
islower() 方法检测字符串是否由小写字母组成。

语法
islower()方法语法:
str.islower()

返回值
如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False

实例
以下实例展示了islower()方法的实例:
#!/usr/bin/python3

str = "RUNOOB example....wow!!!"
print (str.islower())

str = "runoob example....wow!!!"
print (str.islower())

"""
return False

def isnumeric(self): # real signature unknown; restored from __doc__
"""
S.isnumeric() -> bool

Return True if there are only numeric characters in S,
False otherwise.

描述
Python isnumeric() 方法检测字符串是否只由数字组成。这种方法是只针对unicode对象。
注:定义一个字符串为Unicode,只需要在字符串前添加 'u' 前缀即可,具体可以查看本章节例子。

语法
isnumeric()方法语法:
str.isnumeric()

返回值
如果字符串中只包含数字字符,则返回 True,否则返回 False

实例
以下实例展示了isnumeric()方法的实例:
#!/usr/bin/python

str = u"this2009";
print str.isnumeric();

str = u"23443434";
print str.isnumeric();

"""
return False

def isprintable(self): # real signature unknown; restored from __doc__
"""
S.isprintable() -> bool

Return True if all characters in S are considered
printable in repr() or S is empty, False otherwise.

描述
判断是否为可打印字符串

实例
string = "232432a4"
print(string.isprintable())

"""
return False

def isspace(self): # real signature unknown; restored from __doc__
"""
S.isspace() -> bool

Return True if all characters in S are whitespace
and there is at least one character in S, False otherwise.

描述
Python isspace() 方法检测字符串是否只由空格组成。

语法
isspace()方法语法:
str.isspace()

返回值
如果字符串中只包含空格,则返回 True,否则返回 False.

实例
以下实例展示了isspace()方法的实例:
#!/usr/bin/python

str = " ";
print str.isspace();

str = "This is string example....wow!!!";
print str.isspace();

"""
return False

def istitle(self): # real signature unknown; restored from __doc__
"""
S.istitle() -> bool

Return True if S is a titlecased string and there is at least one
character in S, i.e. upper- and titlecase characters may only
follow uncased characters and lowercase characters only cased ones.
Return False otherwise.

描述
Python istitle() 方法检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。

语法
istitle()方法语法:
str.istitle()

返回值
如果字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False.

实例
以下实例展示了istitle()方法的实例:
#!/usr/bin/python

str = "This Is String Example...Wow!!!";
print str.istitle();

str = "This is string example....wow!!!";
print str.istitle();

"""
return False

def isupper(self): # real signature unknown; restored from __doc__
"""
S.isupper() -> bool

Return True if all cased characters in S are uppercase and there is
at least one cased character in S, False otherwise.

描述
Python isupper() 方法检测字符串中所有的字母是否都为大写。

语法
isupper()方法语法:
str.isupper()

返回值
如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False

实例
以下实例展示了isupper()方法的实例:
#!/usr/bin/python

str = "THIS IS STRING EXAMPLE....WOW!!!";
print str.isupper();

str = "THIS is string example....wow!!!";
print str.isupper();

"""
return False

def join(self, iterable): # real signature unknown; restored from __doc__
"""
S.join(iterable) -> str

Return a string which is the concatenation of the strings in the
iterable. The separator between elements is S.

描述
Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

语法
join()方法语法:
str.join(sequence)

参数
sequence -- 要连接的元素序列。

返回值
返回通过指定字符连接序列中元素后生成的新字符串。

实例
以下实例展示了join()的使用方法:
#!/usr/bin/python

str = "-";
seq = ("a", "b", "c"); # 字符串序列
print str.join( seq );

"""
return ""

def ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""
S.ljust(width[, fillchar]) -> str

Return S left-justified in a Unicode string of length width. Padding is
done using the specified fill character (default is a space).

描述
Python ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。
如果指定的长度小于原字符串的长度则返回原字符串。

语法
ljust()方法语法:
str.ljust(width[, fillchar])

参数
width -- 指定字符串长度。
fillchar -- 填充字符,默认为空格。

返回值
返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。
如果指定的长度小于原字符串的长度则返回原字符串。

实例
以下实例展示了ljust()的使用方法:
#!/usr/bin/python

str = "this is string example....wow!!!";

print str.ljust(50, '0');

"""
return ""

def lower(self): # real signature unknown; restored from __doc__
"""
S.lower() -> str

Return a copy of the string S converted to lowercase.

描述
Python lower() 方法转换字符串中所有大写字符为小写。

语法
lower()方法语法:
str.lower()

返回值
返回将字符串中所有大写字符转换为小写后生成的字符串。

实例
以下实例展示了lower()的使用方法:
#!/usr/bin/python

str = "THIS IS STRING EXAMPLE....WOW!!!";

print str.lower();

"""
return ""

def lstrip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.lstrip([chars]) -> str

Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.

描述
Python lstrip() 方法用于截掉字符串左边的空格或指定字符。

语法
lstrip()方法语法:
str.lstrip([chars])

参数
chars --指定截取的字符。

返回值
返回截掉字符串左边的空格或指定字符后生成的新字符串。

实例
以下实例展示了lstrip()的使用方法:
#!/usr/bin/python

str = " this is string example....wow!!! ";
print str.lstrip();
str = "88888888this is string example....wow!!!8888888";
print str.lstrip('8');

以上实例输出结果如下:
this is string example....wow!!!
this is string example....wow!!!8888888
"""
return ""

def maketrans(self, *args, **kwargs): # real signature unknown
"""
Return a translation table usable for str.translate().

If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters to Unicode ordinals, strings or None.
Character keys will be then converted to ordinals.
If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it
must be a string, whose characters will be mapped to None in the result.

描述
maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,
第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
两个字符串的长度必须相同,为一一对应的关系。
注:Python3.4已经没有string.maketrans()了,取而代之的是内建函数: bytearray.maketrans()、
bytes.maketrans()、str.maketrans()

语法
maketrans()方法语法:
str.maketrans(intab, outtab)

参数
intab -- 字符串中要替代的字符组成的字符串。
outtab -- 相应的映射字符的字符串。

返回值
返回字符串转换后生成的新字符串。

实例
以下实例展示了使用maketrans() 方法将所有元音字母转换为指定的数字:
#!/usr/bin/python3

intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)

str = "this is string example....wow!!!"
print (str.translate(trantab))
以上实例输出结果如下:
th3s 3s str3ng 2x1mpl2....w4w!!!

"""
pass

def partition(self, sep): # real signature unknown; restored from __doc__
"""
S.partition(sep) -> (head, sep, tail)

Search for the separator sep in S, and return the part before it,
the separator itself, and the part after it. If the separator is not
found, return S and two empty strings.

描述
从左向右遇到分隔符把字符串分割成两部分,返回头、分割符、
尾三部分的三元组。如果没有找到分割符,就返回头、尾两个空元素的三元组。

实例
s1 = "I'm a good sutdent."
#以'good'为分割符,返回头、分割符、尾三部分。
s2 = s1.partition('good')
#没有找到分割符'abc',返回头、尾两个空元素的元组。
s3 = s1.partition('abc')

print(s1)
print(s2)
print(s3)

结果如下:
I'm a good sutdent.
("I'm a ", 'good', ' sutdent.')
("I'm a good sutdent.", '', '')

"""
pass

def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
"""
S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.

描述
replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),
如果指定第三个参数max,则替换不超过 max 次。

语法
replace()方法语法:
str.replace(old, new[, max])

参数
old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次

返回值
返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,
如果指定第三个参数max,则替换不超过 max 次

实例
以下实例展示了replace()函数的使用方法:
#!/usr/bin/python3

str = "www.w3cschool.cc"
print ("菜鸟教程新地址:", str)
print ("菜鸟教程新地址:", str.replace("w3cschool.cc", "runoob.com"))

str = "this is string example....wow!!!"
print (str.replace("is", "was", 3))
以上实例输出结果如下:
菜鸟教程新地址: www.w3cschool.cc
菜鸟教程新地址: www.runoob.com
thwas was string example....wow!!!

"""
return ""

def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.rfind(sub[, start[, end]]) -> int

Return the highest 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.

Return -1 on failure.

描述
Python rfind() 返回字符串最后一次出现的位置,如果没有匹配项则返回-1。

语法
rfind()方法语法:
str.rfind(str, beg=0 end=len(string))

参数
str -- 查找的字符串
beg -- 开始查找的位置,默认为0
end -- 结束查找位置,默认为字符串的长度。

返回值
返回字符串最后一次出现的位置,如果没有匹配项则返回-1。

实例
以下实例展示了rfind()函数的使用方法:
#!/usr/bin/python3

str1 = "this is really a string example....wow!!!"
str2 = "is"

print (str1.rfind(str2))

print (str1.rfind(str2, 0, 10))
print (str1.rfind(str2, 10, 0))

print (str1.find(str2))
print (str1.find(str2, 0, 10))
print (str1.find(str2, 10, 0))
以上实例输出结果如下:
5
5
-1
2
2
-1
"""
return 0

def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.rindex(sub[, start[, end]]) -> int

Like S.rfind() but raise ValueError when the substring is not found.

描述
rindex() 返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常,
你可以指定可选参数[beg:end]设置查找的区间。

语法
rindex()方法语法:
str.rindex(str, beg=0 end=len(string))

参数
str -- 查找的字符串
beg -- 开始查找的位置,默认为0
end -- 结束查找位置,默认为字符串的长度。

返回值
返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常。

实例
以下实例展示了rindex()函数的使用方法:
#!/usr/bin/python3
str1 = "this is really a string example....wow!!!"
str2 = "is"

print (str1.rindex(str2))
print (str1.rindex(str2,10))
以上实例输出结果如下:
5
Traceback (most recent call last):
File "test.py", line 6, in <module>
print (str1.rindex(str2,10))
ValueError: substring not found

"""
return 0

def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""
S.rjust(width[, fillchar]) -> str

Return S right-justified in a string of length width. Padding is
done using the specified fill character (default is a space).

描述
rjust() 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。
如果指定的长度小于字符串的长度则返回原字符串。

语法
rjust()方法语法:
str.rjust(width[, fillchar])

参数
width -- 指定填充指定字符后中字符串的总长度.
fillchar -- 填充的字符,默认为空格。

返回值
返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。
如果指定的长度小于字符串的长度则返回原字符串

实例
以下实例展示了rjust()函数的使用方法:
#!/usr/bin/python3

str = "this is string example....wow!!!"
print (str.rjust(50, '*'))
以上实例输出结果如下:
******************this is string example....wow!!!

"""
return ""

def rpartition(self, sep): # real signature unknown; restored from __doc__
"""
S.rpartition(sep) -> (head, sep, tail)

Search for the separator sep in S, starting at the end of S, and return
the part before it, the separator itself, and the part after it. If the
separator is not found, return two empty strings and S.



"""
pass

def rsplit(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
"""
S.rsplit(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string, starting at the end of the string and
working to the front. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified, any whitespace string
is a separator.





"""
return []

def rstrip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.rstrip([chars]) -> str

Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.

描述
rstrip() 删除 string 字符串末尾的指定字符(默认为空格).

语法
rstrip()方法语法:
str.rstrip([chars])

参数
chars -- 指定删除的字符(默认为空格)

返回值
返回删除 string 字符串末尾的指定字符后生成的新字符串。

实例
以下实例展示了rstrip()函数的使用方法:
#!/usr/bin/python3

str = " this is string example....wow!!! "
print (str.rstrip())
str = "*****this is string example....wow!!!*****"
print (str.rstrip('*'))
以上实例输出结果如下:
this is string example....wow!!!
*****this is string example....wow!!!

"""
return ""

def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
"""
S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.

描述
split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串

语法
split()方法语法:
str.split(str="", num=string.count(str)).

参数
str -- 分隔符,默认为所有的空字符,包括空格、换行( )、制表符( )等。
num -- 分割次数。

返回值
返回分割后的字符串列表。

实例
以下实例展示了split()函数的使用方法:
#!/usr/bin/python3

str = "this is string example....wow!!!"
print (str.split( ))
print (str.split('i',1))
print (str.split('w'))

以上实例输出结果如下:
['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']
"""
return []

def splitlines(self, keepends=None): # real signature unknown; restored from __doc__
"""
S.splitlines([keepends]) -> list of strings

Return a list of the lines in S, breaking at line boundaries.
Line breaks are not included in the resulting list unless keepends
is given and true.

描述
Python splitlines() 按照行(' ', ' ', ')分隔,返回一个包含各行作为元素的列表,
如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

语法
splitlines()方法语法:
str.splitlines([keepends])

参数
keepends -- 在输出结果里是否去掉换行符(' ', ' ', '),
默认为 False,不包含换行符,如果为 True,则保留换行符。

返回值
返回一个包含各行作为元素的列表。

实例
# 以下实例展示了splitlines()函数的使用方法:
# >>> 'ab c de fg kl '.splitlines()
# ['ab c', '', 'de fg', 'kl']
# >>> 'ab c de fg kl '.splitlines(True)
# ['ab c ', ' ', 'de fg ', 'kl ']
# >>>

"""
return []

def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__
"""
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.

描述
startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,
否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。

语法
startswith()方法语法:
str.startswith(str, beg=0,end=len(string));

参数
str -- 检测的字符串。
strbeg -- 可选参数用于设置字符串检测的起始位置。
strend -- 可选参数用于设置字符串检测的结束位置。

返回值
如果检测到字符串则返回True,否则返回False。

实例
以下实例展示了startswith()函数的使用方法:
#!/usr/bin/python3

str = "this is string example....wow!!!"
print (str.startswith( 'this' ))
print (str.startswith( 'string', 8 ))
print (str.startswith( 'this', 2, 4 ))
以上实例输出结果如下:
True
True
False

"""
return False

def strip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.strip([chars]) -> str

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.

描述
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。

语法
strip()方法语法:
str.strip([chars]);

参数
chars -- 移除字符串头尾指定的字符。

返回值
返回移除字符串头尾指定的字符生成的新字符串。

实例
以下实例展示了strip()函数的使用方法:
#!/usr/bin/python3

str = "*****this is string example....wow!!!*****"
print (str.strip( '*' ))
以上实例输出结果如下:
this is string example....wow!!!

"""
return ""

def swapcase(self): # real signature unknown; restored from __doc__
"""
S.swapcase() -> str

Return a copy of S with uppercase characters converted to lowercase
and vice versa.

描述
swapcase() 方法用于对字符串的大小写字母进行转换。

语法
swapcase()方法语法:
str.swapcase();

参数
NA。

返回值
返回大小写字母转换后生成的新字符串。

实例
以下实例展示了swapcase()函数的使用方法:
#!/usr/bin/python3

str = "this is string example....wow!!!"
print (str.swapcase())

str = "This Is String Example....WOW!!!"
print (str.swapcase())
以上实例输出结果如下:
THIS IS STRING EXAMPLE....WOW!!!
tHIS iS sTRING eXAMPLE....wow!!!

"""
return ""

def title(self): # real signature unknown; restored from __doc__
"""
S.title() -> str

Return a titlecased version of S, i.e. words start with title case
characters, all remaining cased characters have lower case.

描述
Python title() 方法返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。

语法
title()方法语法:
str.title();

返回值
返回"标题化"的字符串,就是说所有单词都是以大写开始。

实例
以下实例展示了 title()函数的使用方法:
#!/usr/bin/python

str = "this is string example from runoob....wow!!!"
print (str.title())
以上实例输出结果如下:
This Is String Example From Runoob....Wow!!!

"""
return ""

def translate(self, table): # real signature unknown; restored from __doc__
"""
S.translate(table) -> str

Return a copy of the string S in which each character has been mapped
through the given translation table. The table must implement
lookup/indexing via __getitem__, for instance a dictionary or list,
mapping Unicode ordinals to Unicode ordinals, strings, or None. If
this operation raises LookupError, the character is left untouched.
Characters mapped to None are deleted.

描述
translate() 方法根据参数table给出的表(包含 256 个字符)转换字符串的字符,
要过滤掉的字符放到 deletechars 参数中。

语法
translate()方法语法:
str.translate(table[, deletechars]);
bytes.translate(table[, delete])
bytearray.translate(table[, delete])

参数
table -- 翻译表,翻译表是通过 maketrans() 方法转换而来。
deletechars -- 字符串中要过滤的字符列表。

返回值
返回翻译后的字符串,若给出了 delete 参数,则将原来的bytes中的属于delete的字符删除,
剩下的字符要按照table中给出的映射来进行映射 。

实例
以下实例展示了 translate() 函数的使用方法:
实例(Python 3.0+)
#!/usr/bin/python3

intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab) # 制作翻译表

str = "this is string example....wow!!!"
print (str.translate(trantab))

以上实例输出结果如下:
th3s 3s str3ng 2x1mpl2....w4w!!!

以下实例演示如何过滤掉的字符 x 和 m:
实例(Python 3.0+)
#!/usr/bin/python

# 制作翻译表
bytes_tabtrans = bytes.maketrans(b'abcdefghijklmnopqrstuvwxyz', b'ABCDEFGHIJKLMNOPQRSTUVWXYZ')

# 转换为大写,并删除字母o
print(b'runoob'.translate(bytes_tabtrans, b'o'))
以上实例输出结果:
b'RUNB'
"""
return ""

def upper(self): # real signature unknown; restored from __doc__
"""
S.upper() -> str

Return a copy of S converted to uppercase.

描述
Python upper() 方法将字符串中的小写字母转为大写字母。

语法
upper()方法语法:
str.upper()

返回值
返回小写字母转为大写字母的字符串。

实例
以下实例展示了 upper()函数的使用方法:
#!/usr/bin/python3

str = "this is string example from runoob....wow!!!";

print ("str.upper() : ", str.upper())
以上实例输出结果如下:
str.upper() : THIS IS STRING EXAMPLE FROM RUNOOB....WOW!!!

"""
return ""

def zfill(self, width): # real signature unknown; restored from __doc__
"""
S.zfill(width) -> str

Pad a numeric string S with zeros on the left, to fill a field
of the specified width. The string S is never truncated.

描述
Python zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0。

语法
zfill()方法语法:
str.zfill(width)

参数
width -- 指定字符串的长度。原字符串右对齐,前面填充0。

返回值
返回指定长度的字符串。

实例
以下实例展示了 zfill()函数的使用方法:
#!/usr/bin/python3

str = "this is string example from runoob....wow!!!"
print ("str.zfill : ",str.zfill(40))
print ("str.zfill : ",str.zfill(50))
以上实例输出结果如下:
str.zfill : this is string example from runoob....wow!!!
str.zfill : 000000this is string example from runoob....wow!!!

"""
return ""

def __add__(self, *args, **kwargs): # real signature unknown
""" Return self+value. """
pass

def __contains__(self, *args, **kwargs): # real signature unknown
""" Return key in self. """
pass

def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass

def __format__(self, format_spec): # real signature unknown; restored from __doc__
"""
S.__format__(format_spec) -> str

Return a formatted version of S as described by format_spec.
"""
return ""

def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass

def __getitem__(self, *args, **kwargs): # real signature unknown
""" Return self[key]. """
pass

def __getnewargs__(self, *args, **kwargs): # real signature unknown
pass

def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass

def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass

def __hash__(self, *args, **kwargs): # real signature unknown
""" Return hash(self). """
pass

def __init__(self, value='', encoding=None, errors='strict'): # known special case of str.__init__
"""
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
# (copied from class doc)
"""
pass

def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass

def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass

def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass

def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass

def __mod__(self, *args, **kwargs): # real signature unknown
""" Return self%value. """
pass

def __mul__(self, *args, **kwargs): # real signature unknown
""" Return self*value.n """
pass

@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass

def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass

def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass

def __rmod__(self, *args, **kwargs): # real signature unknown
""" Return value%self. """
pass

def __rmul__(self, *args, **kwargs): # real signature unknown
""" Return self*value. """
pass

def __sizeof__(self): # real signature unknown; restored from __doc__
""" S.__sizeof__() -> size of S in memory, in bytes """
pass

def __str__(self, *args, **kwargs): # real signature unknown
""" Return str(self). """
pass
原文地址:https://www.cnblogs.com/changha0/p/7907872.html