Python3 - Str类型总结

1.str类型功能分类

1.1 文本处理

capitalize(self)
# 首字母大写
'''
return: 新字符串
'''
s = "hello"
cap = s.capitalize()
#输出:H

#-----------

swapcase(self)
# 大小写互换
'''
return: 新字符串
'''

  

1.2 格式处理

#------------

center(self, width, fillchar=None)
# 重新设置字符串长度,字符串居中,默认填充空格。
'''
param width: 设置字符串长度
param fillchar: 填充内容
return: 新字符串
'''
cen = s.center(20)
#输出       hello      

#------------

expandtabs(self, tabsize=None)
# 字符串中占位符\t,转换成空格,默认tab表示八个空格。
# \t 表示 tab。
'''
param tabsize: 设置空格长度
return: 新字符串
'''
s = "he\tllo"
a = s.expandtabs(tabsize=2)
#输出:he  llo

#------------

ljust(self, width, fillchar=None)
rjust(self, width, fillchar=None)
# 左或右对齐,余下部分默认填充空格。

#------------

title(self)
# 字符串转标题(字符串首字母大写)

#------------

zfill(self, width)
#设置字符串长度,右对齐,余下长度填充0。

#------------

1.3 编码与解码

decode(self, encoding=None, errors=None): 
encode(self, encoding=None, errors=None): 
#encode unicode -转- str
#decode str -转 - unicode
#py字符串都是unicode形式的。
'''
param encoding: 转换使用编码
param errors: 异常
return: 对象
'''

1.4 增

join(self, iterable)
# 必须传入一个迭代器,把每个元素连接成字符串。
'''
param iterable: 迭代器(可迭代对象list tuple str)
return: 元素之间用字符串连接。
'''
lst = ["A", "B", "C"]
tpl = ("A", "B", "C")
string = "ABC"
s = "+" #字符串
jn_1 = s.join(lst) 
jn_2 = s.join(tpl)
s_1 = s.join(string)
print(jn_1, jn_2, s_1)
#输出:均为"A+B+C"

1.5 改 & 删

strip(self, chars=None)
Lstrip( )
Rstrip( ) 
# 删除字符串左右两侧指定字符
# L表示最左边,R表示最右边。
# strip 剥皮 
# 注意:如果在边界有相同字符,一并都剥掉。看例子。
'''
param : 删除字符串边界指定字符,参数为空时,默认去除空格。
return: 新字符串
'''
s = "hhhelloh"
lst = s.strip("h")
#输出:hello

#---------------

split(str, num=string.count(str))
# 把标识字符串往前的字符串都分割出来,存入数组返回。
'''
param str: 分割标识字符串,默认按照空格、\n分割。
param num: 分割次数
return: list数组
'''
s = "hello"
lst = s.split("e", 1)
#输出:['h', 'llo']

#---------------

partition(self, sep):
rpartition(self, sep):
# r表示从右向左开始分割
# 根据指定的分隔符将字符串进行分割,和split区别它会返回元组。
'''
param sep: 分割标识字符串,默认按照空格、\n分割。
return: 元组
'''
h = "https://www.baidu.com/"
t = h.partition("://")
#输出:('https', '://', 'www.baidu.com/')

#---------------

splitlines([keepends])
#换行符分割
#http://www.runoob.com/python/att-string-splitlines.html

#---------------

replace(self, old, new, count=None)
#替换
'''
param old: 待替换字符
param new: 参与替换字符
param count: 替换次数, 缺省全部替换
return: 新字符串
'''
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);
#输出:thwas was string example....wow!!! thwas was really string
#输出:thwas was string example....wow!!! thwas is really string

1.6 查 

  1. 基本查询方式:
  • 索引访问:字符串可以看成一个list。
  • 切片: s[start:end] 区间为左开右闭。或 s[:1] 闭区间 0 至 1。count(self, sub, start=None, end=None):# 查找字符串片段(s[start:end])在目标字符串出现次数
count(self, sub, start=None, end=None):
# 查找字符串片段(s[start:end])在目标字符串出现次数
'''
param sub:目标字符串 
param start: 起始位置 
param end: 终止位置
return: 出现次数
'''
s = "hello"
c1 = s.count("h")
c2 = s.count("ll")
print(c1, c2)
#输出: 1 1

#-----------

endswith(self, suffix, start=None, end=None): 
# 判断是否以某字符串结束
'''
param suffix: 参与匹配的字符串
param start: 起始位置
param end: 终止位置
return: 布尔值
'''
s = "hello"
a = s.endswith("o")
#输出:True

#-----------

find(self, sub, start=None, end=None)
rfind(self, sub, start=None, end=None)从右向左查询。(节约资源?)
# 查找字符串
# 字符串可以看成list。索引从 0 开始
'''
param sub: 目标字符串
param start: 起始位置
param end: 终止位置
return: 未找到返回-1,找到返回位置。
'''
s = "he\tllo"
a = s.find("h")
#输出: 0

#----------

index(self, sub, start=None, end=None)
rindex(self, sub, start=None, end=None) 从右向左查询
# 查找字符串,与find的一样。仅有区别是未找到会报错。

#----------

isalnum(self) #判断字符串是否为数字和字母
isalpha(self) #判断字符串是否为字母
isdigit(self) #判断字符串是否为数字
islower(self) #判断字符串是否为小写
isupper(self) #判断字符串是否为大写  
isspace(self) #判断字符串是否为空格
istitle(self) #判断字符串是否为标题(首字母大写)

#----------

startswith(self, prefix, start=None, end=None)
# 是否为起始字符串
'''
param prefix: 参与检测字符串片段
param start: 起始位置
param end: 终止位置
return: 布尔值
'''

  

  

 

posted @ 2017-05-12 19:58  lugou  阅读(136)  评论(0)    收藏  举报