Python中字符串的常用操作

首先我们创建一个字符串str1:

str1='good good study,day day up'

1. find

检查str是否包含在str1中,如果是返回开始的索引值,否则返回-1

str1="good good study,day day up"

index = str1.find("stu")

print(index)

运行结果:

10

说明"stu”是在索引值为10的位置查询到的

2. index

跟find()方法一样,只不过目标字符串如果不在要查找的字符串中,会报一个异常

3. count

返回str在目标字符串中start-end之间出现的次数

str1="good good study,day day up"

print(str1.count("good"))

运行结果:

2

说明"good"在str1中出现了2次

4. replace

把str1中指定的字符串"good",用“222”进行替换,最多替换2次

str1="good good study,day day up"

print(str1.count("good"))

str2 = str1.replace("good","222",2)

运行(str2)

运行结果:

2

222 222 study,day day up

5. split

以" "为分割符切片str1

str1="good good study,day day up"

array = str1.split(" ")

print(type(array))

print(array)

运行结果:

['good', 'good', 'study,day', 'day', 'up']

说明:此时经过以空格切片分割后的结果返回的是一个列表

6. capitalize

把字符串的第一个字符大写

str1="good good study,day day up"

print(str1.capitalize())

运行结果:

Good good study,day day up

7. title

把字符串中的每一个单词的首字母大写

str1="good good study,day day up"

print(str1.title())

运行结果:

Good Good Study,Day Day Up

8. startswith

检查字符串是否以指定字符串开头,是则返回True,否则返回False

例:判断是否为有效网址

str1 = "www.baidu.co"

print(str1.startswith('www'))

运行结果:

True

9. endswith

检查字符串是否以指定字符串开头,是则返回True,否则返回False

例:判断是否为有效指定邮箱格式

str1 = "[email protected]"

print(str1.endswith('@qq.com'))

运行结果:

True

10. lower

转换字符串中所有的大写字符为小写

str1="Good good study,day day up"

str2 = str1.lower()

print(str1)

print(str2)

运行结果:

Good good study,day day up

good good study,day day up

11. upper

转换字符串中所有的小写字符为大写

str1="Good good study,day day up"

str2 = str1.upper()

print(str1)

print(str2)

运行结果:

Good good study,day day up

GOOD GOOD STUDY,DAY DAY UP

12. ljust

返回一个原字符串左对齐,并使用空格填充至长度width的新字符

str1 = "hello"

print(str1.ljust(10))

运行结果:

hello     

注:hello右边有5个空格

13. rjust

返回一个原字符串右对齐,并使用空格填充至长度width的新字符

str1="hello"

print(str1.rjust(10))

运行结果:

     hello

注:hello左边有5个空格

14. center

返回一个原字符串居中对齐,并使用空格填充至长度width的新字符

str1="hello"

print(str1.center(10))

运行结果:

  hello   

注:hello左边2个空格,右边3个空格

15. lstrip

删除目标字符串左边的空格

str1 ="     hello     "

print(str1.lstrip())

运行结果:

hello     

注:hello右边仍有空格

16. rstrip

删除目标字符串右边的空格

str1 ="     hello     "

print(str1.rstrip())

运行结果:

     hello

注:hello左边仍有空格

17. strip

删除目标字符串两边的空格

str1 ="    hello    "

print(str1.strip())

运行结果:

hello

18. rfind

类似于find()函数,不过是从右边开始查找

19. rindex

类似与index()函数,不过是从右边开始查找

20. partition

把目标字符串分割成str前,str以及str后三部分,得到一个tuple(元组)

str1="nihaoma"

print(str1.partition("hao"))

运行结果:

('ni', 'hao', 'ma')

注;元祖,是小括号内包含两个以上的元素比如,(1,2,"a")或(1,)

21. rpartition

从右边开始,把目标字符串分割成str前,str以及str后三部分,得到一个

tuple(元组)

str1="nihaomahao"

print(str1.partition("hao"))

运行结果:

('nihaoma', 'hao', '')

('ni', 'hao', 'ma')

22. splitlines

将目标字符串按照行进行分割,返回一个列表

str1 = "Hello\nWorld"

print(str1.splitlines())

运行结果:

['Hello', 'World']

23. isalpha

判断目标字符串中是否所有的字符都为字母,返回True,或者False

str1 = "123Hello"

print(str1.isalpha())

运行结果:

False

24. isdigit

判断目标字符串中是否所有的字符都为数字,返回True或者False

str1 = "123Hello"

print(str1.isdigit())

运行结果:

False

25. isalnum

如果字符串中是由字母或者数字组成(不能有符号等),则返回True,否则返回False

str1="abc123"

print(str1.isalnum())

运行结果:

True

26. isspace

如果字符串中只包含空格,则返回True,否则返回False

li="      "

str2 = li.isspace()

print(str2)

运行结果:

True

27. join

将字符串或者列表,元组中的每个元素(字符)使用指定字符连接起来

li=["one","two","three"]

str2="_"

str2 = str2.join(li)

print(str2)

运行结果:

one_two_three

你可能感兴趣的:(Python中字符串的常用操作)