python字符串常用内建函数总结
2019-01-23 07:41:53来源:博客园 阅读 ()
自己总结一些常用字符串函数,理解比较粗糙
1.字符串内建函数-大小写转换函数
(1)str.capitalize
Help on method_descriptor:
capitalize(...)
S.capitalize() -> str
Return a capitalized version of S, i.e. make the first character
have upper case and the rest lower case.
返回值首字母大写,其余小写,不改变数据本身
实例:
a = “start”
a.caplitalize()
Start
(2)str.title()
Help on method_descriptor:
title(...)
S.title() -> str
Return a titlecased version of S, i.e. words start with title case
characters, all remaining cased characters have lower case
返回值首字母大写,其余都小写
实例:
a = “start”
a.title()
Start
a = “sTART”
a.title()
Start
(3)str.lower()
Help on method_descriptor:
lower(...)
S.lower() -> str
Return a copy of the string S converted to lowercase.
返回值字母大写转换为小写,大转小
实例:
a = “START”
a.lower()
start
(4)str.upper()
Help on method_descriptor:
upper(...)
S.upper() -> str
Return a copy of S converted to uppercase.
返回值字母小写转换为大写,小转大
实例:
a = “start”
a.upper()
START
(5)str.swapcase()
Help on method_descriptor:
swapcase(...)
S.swapcase() -> str
Return a copy of S with uppercase characters converted to lowercase
and vice versa.
返回值字母大写转换成小写,小写转换成大写
实例:
a = “Start”
a.swapcase()
sTART
2.字符串内建函数-搜索函数
(1)str.find()
Help on method_descriptor:
find(...)
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.
S.find(sub[, start[, end]])
搜索字符串在指定的索引范围是否包含子字符串的索引值,否则返回-1
参数:
sub –指定索引的字符串
start -- 开始索引,默认为0。
end -- 结束索引,默认为字符串的长度
实例:
a = “start”
b = “hd”
c = “ar”
a.find(b)
-1
a.find(c)
2
(2)str.index()
Help on method_descriptor:
index(...)
S.index(sub[, start[, end]]) -> int
Like S.find() but raise ValueError when the substring is not found.
搜索字符串在指定的索引范围是否包含子字符串的索引值,与find方法一样,只不过如果str不在字符串中回报一个异常
实例:
a = “start”
b = “hd”
c = “ar”
a.index(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
a.index(c)
2
(3)str.count()
Help on method_descriptor:
count(...)
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.
计算子字符串在指定字符串中出现的次数
实例:
a = “start”
b = “a”
a.count(b)
1
(4)str.endswith()
Help on method_descriptor:
endswith(...)
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.
如果字符串含有指定的后缀返回True,否则返回False
实例:
a = “start”
b = “a”
a.endswith(b)
False
a = “start”
b = “t”
a.endswith(b)
True
3.字符串内建函数-替换函数
(1)str.replace(old,new)
Help on method_descriptor:
replace(...)
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.
将old替换为new
实例:
a = “start”
b = “op’
a.replace(‘art’, b)
‘stop’
(2)str.strip(char)
Help on method_descriptor:
strip(...)
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.
在str的开头和结尾删除char,当char为空时,默认删除空白符
实例:
a = “ start ”
a.strip()
“start”
(3)str.rstrip()
Help on method_descriptor:
rstrip(...)
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.
删除str字符串末尾的空格,或换行符号
实例:
a = “ start ”
a.rstrip()
“ start”
原文链接:https://www.cnblogs.com/only-me/p/10303248.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:#4 编写第一个程序
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash