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

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

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 "", line 1, in

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”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值