Python字符串

Python capitalize()方法

描述:
Python capitalize()将字符串的第一个字母变成大写,其他字母变小写。对于 8 位字节编码需要根据本地环境。

capitalize()方法语法
str.capitalize()

返回值:

s = 'a, B'
s.capitalize()
s = ' a, B'    # a 前面有空格
s.capitalize()
s = 'a, BCD'
s.capitalize()

输出结果:

'A, b'
' a, b'
'A, bcd'

Python center()方法

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

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

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

参数:

width – 字符串的总宽度。
fillchar – 填充字符。

str = 'xiaohong'
str.center(20, '*')
str.center(20)

输出结果:

******xiaohong******
'      xiaohong      '

Python count()方法

描述:
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)

输出结果:

str.count(sub, 4, 40) :  2
str.count(sub) :  1

Python decode()方法

描述:
Python decode() 方法以 encoding 指定的编码格式解码字符串。默认编码为字符串编码。

decode()方法语法
str.decode(encoding=‘UTF-8’,errors=‘strict’)

返回值:
该方法返回解码后的字符串。如果字符串含有指定的后缀返回True,否则返回False。如果字符串含有指定的后缀返回True,否则返回False。如果字符串含有指定的后缀返回True,否则返回False。

encoding – 要使用的编码,如"UTF-8"。
errors – 设置不同错误的处理方案。默认为 ‘strict’,意为编码错误引起一个UnicodeError。 其他可能得值有 ‘ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ 以及通过 codecs.register_error() 注册的任何值。

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() 注册的任何值。

返回值:
该方法返回编码后的字符串。

实例:

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]])

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

参数:

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

实例:

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)

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

参数
tabsize – 指定转换字符串中的 tab 符号(’\t’)转为空格的字符数。

实例:

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))

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

参数:

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

实例

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

原文参考:菜鸟教程

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值