美文网首页
常用字符串方法

常用字符串方法

作者: Vector_Wan | 来源:发表于2019-04-28 13:07 被阅读0次

常用:find( ) replace( ) split( ) join( ) strip( )

  • 查看字符串方法:
print(dir(''))

1. chr 的替换: replace( )

  • 描述

Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次,默认是全部替换。这个方法不是 inplace方法,不会修改元字符串。

  • 语法
str.replace(old, new, max)
  • 参数

old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次

  • 返回值

返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。

  • 实例:¥599 -> 599
name = 'hello world ha ha'
new_name = name.replace('ha', 'Ha',1)   # 参数含义(老字符, 新字符, 替换数量)
new_name
# 'hello world Ha ha'

2. 字符串分割成 chr 列表: split( )

  • 描述

Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则将字符串分隔成 num+1 个子字符串

  • 语法
str.split(str="", num=string.count(str))
  • 参数

str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num -- 分割次数。默认为 -1, 即分隔所有。

  • 返回值

返回分割后的字符串列表。

  • 实例
name = 'hello ha ha ha'
name_list = name.split(" ")
print(name_list)
name_list1 = name.split(' ', 2)
print(name_list1)
# ['hello', 'ha', 'ha', 'ha']
# ['hello', 'ha', 'ha ha']

3. 列表连接成字符串:join( )

  • 描述

Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

  • 语法
str.join(sequence)
  • 参数

sequence -- 要连接的元素序列。

  • 返回值

返回通过指定字符连接序列中元素后生成的新字符串。

  • 实例
str4 = ' '
list1 = ['my', 'name', 'is', 'vector']
str4.join(list1)
# 'my name is vector'
'_'.join(list1)
# 'my_name_is_vector'
'$'.join(list1)
# 'my$name$is$vector'

4. chr 的查找:find( ) 与 rfind( )

  • 描述

Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。rfind()方法就是从右侧(索引为 -1)开始检查。

  • 语法
str.find(str, beg=0, end=len(string))
  • 参数

str -- 指定检索的字符串
beg -- 开始索引,默认为0。
end -- 结束索引,默认为字符串的长度。

  • 返回值

如果包含子字符串返回开始的索引值,否则返回-1。

  • 实例
my_str = 'hello world neuq and neuducpp'
my_str.rfind('neuq')
# 12

5. 空白的去除:strip( ) 与 rstrip( ) lstrip( )

  • 描述

Python strip() 方法用于移除字符串头尾指定的字符(无参数默认为空格或换行符)或字符序列。
注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
rstrip( )方法可以清除字符串右边的空白字符。

  • 语法
str.strip([chars])
  • 参数

chars -- 移除字符串头尾指定的字符序列。(默认为空白)

  • 返回值

返回移除字符串头尾指定的字符生成的新字符串。

  • 实例
# 含有参数:
print('oooooooohello      o'.lstrip('o'))
>> hello      o
# 无参数
my_str_space = 'hello'
print(len(my_str_space.center(20)))
my_str_space.center(20).strip()
>> 20
>> 'hello'
str1 = '\n\thahaha\t'
str1.strip()
>> 'hahaha'
my_str_space.center(20).lstrip()
>> 'hello        '
my_str_space.center(20).lstrip().rstrip()
>> 'hello'

6. 将字符串首字母大写

str.capitalize()

my_str = 'hello world neuq and neuducpp'
my_str.capitalize()
# 'Hello world neuq and neuducpp'

7. 把字符串每个单词首字母大写

str.title()

my_str = 'hello world neuq and neuducpp'
my_str.title()
# 'Hello World Neuq And Neuducpp'

8. 检测是否以指定的 str 开头

str.startswith('str')

my_str = 'hello world neuq and neuducpp'
my_str.startswith('hello')
# True
my_str.startswith('Haello')
# False

9. 检测字符串是否以 str 结尾

endswith( )

my_str = 'hello world neuq and neuducpp'
my_str.endswith('cpp')
# True

10. 把字符串转化为大写

str.upper( )

my_str = 'hello world neuq and neuducpp'
my_str.upper()
# 'HELLO WORLD NEUQ AND NEUDUCPP'

11. 把字符串所有字母变为小写

str.lower( )

my_str.upper().lower()
# 'hello world neuq and neuducpp'

12. ljust( ) & rjust( )

返回一个原字符串左对齐(右对齐)并用空格填充。 参数表示字符串的宽度。

my_str_space = 'hello'
my_str_space.ljust(10)
# 'hello     '
len(my_str_space.ljust(10))
# 10
my_str_space.rjust(10)
# '     hello'
my_str_space.ljust(5).rjust(20)
# '               hello'

13. center

my_str_space.center(20)
#  '       hello        '

14. index( ) 与 rindex( )

15. partition(str)

把 my_str 以 str 分割成三部分:str 前、str、str 后。以元组的形式返回.

my_str.partition('neuq')
# ('hello world ', 'neuq', ' and neuducpp')

16. rpartition()

从右边开始。

17. splitlines()

按照行分割返回一个包含各行作为元素的列表。

line = 'hello\nworld'
print(line)
line.splitlines()
# hello
# world
# ['hello', 'world']

18. isalpha()

判断字符串是否都是由字母组成。

my_str.isalpha()
# False
alpha = 'aaaaaa'
alpha.isalpha()
True

19. isdigit

判断是否是数字

20. isalnum()

判断是否只有字母和数字

21. isspace( )

判断是否只有空格。

相关文章

网友评论

      本文标题:常用字符串方法

      本文链接:https://www.haomeiwen.com/subject/dbddnqtx.html