前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python字符串常用内建函数总结

python字符串常用内建函数总结

作者头像
py3study
发布2020-01-17 11:31:51
6410
发布2020-01-17 11:31:51
举报
文章被收录于专栏:python3

自己总结一些常用字符串函数,理解比较粗糙

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”

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/05/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档