Home >Backend Development >Python Tutorial >Python字符串处理函数简明总结

Python字符串处理函数简明总结

WBOY
WBOYOriginal
2016-06-06 11:24:401316browse

返回被去除指定字符的字符串

默认去除空白字符

删除首尾字符:str.strip([char])
删除首字符:str.lstrip([char])
删除尾字符str.strip([char])

判断是否匹配首末字符

匹配成功返回True,否则返回False
匹配首字符:str.startswith(char[, start[, end]])
匹配末字符:str.endswith(char[, start[, end]])

查找字符,找到返回字符位置,否则返回-1

从字符串开头查找str.find(char[, start[, end]])
从字符串末端开始查找str.find(char[, start[, end]])

分解字符

str.split([sep [, maxsplit]])

连接字符

str.join(iterable)
“str”是连接字符,“iterable”是被连接的可迭代字符串

替换字符

str.replace(old, new [,count])
返回替换后的字符串,count标识替换的次数,默认所有的字符都被替换

按换行符分解字符串,返回行列表

str.splitlines([keepends])
结果不保留换行符,除非keepends为True。

大小写转换

大小写互换:str.swapace()
转换为大写:str.upper()
转换为小写:str.lower()

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn