Python字符串有关的方法汇总

Python capitalize()方法

描述
        Python capitalize()将字符串的第一个字母变成大写,其他字母变小写。对于 8 位字节编码需要根据本地环境。
语法
        capitalize()方法语法:
        str.capitalize()
参数
        无。
返回值
        该方法返回一个首字母大写的字符串。


Python center()方法

描述
        Python center() 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格。
语法
        center()方法语法:
        str.center(width[, fillchar])
参数
        width -- 字符串的总宽度。
        fillchar -- 填充字符。
返回值
        该方法返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。




Python cmp() 函数

描述
        cmp(x,y) 函数用于比较2个对象,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1。
语法
        以下是 cmp() 方法的语法:
        cmp( x, y )
参数
        x -- 数值表达式。
        y -- 数值表达式。
返回值
        如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1。


Python count()方法

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


Python decode()方法

描述
        Python decode() 方法以 encoding 指定的编码格式解码字符串。默认编码为字符串编码。
语法
        decode()方法语法:
        str.decode(encoding='UTF-8',errors='strict')
参数
        encoding -- 要使用的编码,如"UTF-8"。
        errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。
返回值
        该方法返回解码后的字符串。
实例
        以下实例展示了decode()方法的实例:
#!/usr/bin/python
str = "this is string example....wow!!!";
str = str.encode('base64','strict');
print "Encoded String: " + str;
print "Decoded String: " + str.decode('base64','strict')
以上实例输出结果如下:
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!




Python encode()方法

描述
        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')
以上实例输出结果如下:
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=




Python endswith()方法

描述
        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);
以上实例输出结果如下:
True
True
True
False


Python expandtabs()方法

描述
        Python expandtabs() 方法把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8。
语法
        expandtabs()方法语法:
        str.expandtabs(tabsize=8)
参数
        tabsize -- 指定转换字符串中的 tab 符号('\t')转为空格的字符数。
返回值
        该方法返回字符串中的 tab 符号('\t')转为空格后生成的新字符串。
实例
        以下实例展示了expandtabs()方法的实例:
#!/usr/bin/python
str = "this is\tstring example....wow!!!";


print "Original string: " + str;
print "Defualt exapanded tab: " +  str.expandtabs();
print "Double exapanded tab: " +  str.expandtabs(16);
以上实例输出结果如下:
Original string: this is        string example....wow!!!
Defualt exapanded tab: this is string example....wow!!!
Double exapanded tab: this is         string example....wow!!!



Python find()方法

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

语法
        find()方法语法:
        str.find(str, beg=0, end=len(string))
参数
        str -- 指定检索的字符串
        beg -- 开始索引,默认为0。
        end -- 结束索引,默认为字符串的长度。
返回值
        如果包含子字符串返回开始的索引值,否则返回-1。

实例
以下实例展示了find()方法的实例:


实例(Python 2.0+)
#!/usr/bin/python
str1 = "this is string example....wow!!!";
str2 = "exam";
 
print str1.find(str2);
print str1.find(str2, 10);
print str1.find(str2, 40);
以上实例输出结果如下:
15
15
-1
实例(Python 2.0+)
>>>info = 'abca'
>>> print info.find('a')    # 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0
0
>>> print info.find('a',1)  # 从下标1开始,查找在字符串里第一个出现的子串:返回结果3
3
>>> print info.find('3')    # 查找不到返回-1
-1
>>>


Python index()方法

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


语法
        index()方法语法:
        str.index(str, beg=0, end=len(string))
参数
        str -- 指定检索的字符串
        beg -- 开始索引,默认为0。
        end -- 结束索引,默认为字符串的长度。
返回值
        如果包含子字符串返回开始的索引值,否则抛出异常。


Python isdecimal()方法

描述
        Python isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。
        注意:定义一个十进制字符串,只需要在字符串前添加 'u' 前缀即可。
语法
        isdecimal()方法语法:
        str.isdecimal()
参数
        无
返回值
        如果字符串是否只包含十进制字符返回True,否则返回False。


Python len()方法

描述
        Python len() 方法返回对象(字符、列表、元组等)长度或项目个数。
语法
        len()方法语法:
        len( s )
参数
        s -- 对象。
返回值
        返回对象长度。


Python ljust()方法

描述
        Python ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
语法
        ljust()方法语法:
        str.ljust(width[, fillchar])
参数
        width -- 指定字符串长度。
        fillchar -- 填充字符,默认为空格。
返回值
        返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。


Python lower()方法

描述
        Python lower() 方法转换字符串中所有大写字符为小写。
语法
        lower()方法语法:
        str.lower()
参数
        无。
返回值
        返回将字符串中所有大写字符转换为小写后生成的字符串。


Python lstrip()方法

描述
        Python lstrip() 方法用于截掉字符串左边的空格或指定字符。
语法
        lstrip()方法语法:
        str.lstrip([chars])
参数
        chars --指定截取的字符。
返回值
        返回截掉字符串左边的空格或指定字符后生成的新字符串。


Python rstrip()方法

描述
        Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格).
语法
        rstrip()方法语法:
        str.rstrip([chars])
参数
        chars -- 指定删除的字符(默认为空格)
返回值
        返回删除 string 字符串末尾的指定字符后生成的新字符串。


Python splitlines()方法

描述
        Python splitlines() 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
语法
        splitlines()方法语法:
        str.splitlines([keepends])
参数
        keepends -- 在输出结果里是否去掉换行符('\r', '\r\n', \n'),默认为 False,不包含换行符,如果为 True,则保留换行符。
返回值
        返回一个包含各行作为元素的列表。


Python startswith()方法

描述
        Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
语法
        startswith()方法语法:
        str.startswith(str, beg=0,end=len(string));
参数
        str -- 检测的字符串。
        strbeg -- 可选参数用于设置字符串检测的起始位置。
        strend -- 可选参数用于设置字符串检测的结束位置。
返回值
        如果检测到字符串则返回True,否则返回False。


Python strip()方法

描述
        Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。
语法
        strip()方法语法:
        str.strip([chars]);
参数
        chars -- 移除字符串头尾指定的字符。
返回值
        返回移除字符串头尾指定的字符生成的新字符串。


Python swapcase()方法

描述
        Python swapcase() 方法用于对字符串的大小写字母进行转换。
语法
        swapcase()方法语法:
        str.swapcase();
参数
        NA。
返回值
        返回大小写字母转换后生成的新字符串。




Python title()方法

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


Python translate()方法

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

语法
        translate()方法语法:
        str.translate(table[, deletechars]);
参数
        table -- 翻译表,翻译表是通过maketrans方法转换而来。
        deletechars -- 字符串中要过滤的字符列表。
返回值
        返回翻译后的字符串。
实例
        以下实例展示了 translate()函数的使用方法:


#!/usr/bin/python
from string import maketrans   # 引用 maketrans 函数。
intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

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

print str.translate(trantab);


以上实例输出结果如下:

th3s 3s str3ng 2x1mpl2....w4w!!!


以下实例去除字符串中的 'x' 和 'm' 字符:
#!/usr/bin/python
from string import maketrans   # Required to call maketrans function.

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

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

print str.translate(trantab, 'xm');

以上实例输出结果:

th3s 3s str3ng 21pl2....w4w!!!



Python upper()方法

描述
        Python upper() 方法将字符串中的小写字母转为大写字母。
语法
        upper()方法语法:
        str.upper()
参数
        NA。
返回值
        返回小写字母转为大写字母的字符串。
    

Python zfill()方法

描述
        Python zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0。
语法
        zfill()方法语法:
        str.zfill(width)
参数
        width -- 指定字符串的长度。原字符串右对齐,前面填充0。
返回值

        返回指定长度的字符串。



猜你喜欢

转载自blog.csdn.net/baidu_20351223/article/details/79603685
今日推荐