欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-30 11:12:00
自己总结一些常用字符串函数,理解比较粗糙 1.字符串内建函数-大小写转换函数(1)str.capitalizeHelp on method_descriptor:capitalize(...) S.capitalize() -> str Return a capitalized version of... ......

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

 

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”