python中的字符串操作

摘要:本文主要介绍了python中字符串的一些基本操作。

1、切片操作

所谓切片操作就是在给定的字符串中有规律的选取部分元素组成新串的操作

采用  str[参数1:参数2:参数3]  的形式进行操作:

  • 参数1:起始位置下标,默认为第一个位置
  • 参数2:终点位置,需要指定,默认不包含以参数2位下标的元素
  • 参数3:步长,默认为1,会加在第一个参数上完成相应的操作
  • 三个参数均可以是负数,前面两个参数为负数,是表明位置从最后一个元素开始算:-1,-2,-3......
  • 参数1,2是有方向的数对,这个方向应该和参数3相匹配,否则不会输出相应的序列
 1 password='0123456789'
 2 
 3 print(password[0:5:1])  #01234------基本操作
 4 print(password[1:5:2])  #13---------步长操作
 5 
 6 print(password[:5:])    #01234------默认操作
 7 
 8 print(password[-1:-11:-1])  #9876543210--------逆序操作
 9 print(password[::-1])       #9876543210--------默认逆序操作
10 
11 print(password[-2:-9:-2])   #8642---------逆序步长
12 print(password[-9:-2:1])    #1234567------正序操作
13 
14 print(password[0:8:-2])    #没有输出
15 print(password[-2:-9:1])   #没有输出

2、查找操作

在python中查找操作一般常用的有:find、index、count、rfind、rindex

find(‘目标子串’,位置1,位置2) 

  • 返回值:如果找到返回位置,如果没找到返回-1
  • 后面两个参数省略时,就在整个字符串寻找目标子串

index:

  • 和find一样,只有一点区别:没有找到相应的子串,那么就报错

count:

  • 返回值:被查找子串的个数

rfind:

  • 和find一样,只不过是从右侧开始查找

rindex:

  • 和index一样,只不过是从右侧开始查找
 1 str='hello python nice to meet you'
 2 
 3 print(str.find('nice'))  #13
 4 print(str.find('nice',10,17))  #13
 5 print(str.find('hi'))  #-1
 6 
 7 print(str.index('nice'))  #13
 8 print(str.index('nice',10,17))  #13
 9 # print(str.index('hi'))  # erroy
10 
11 print(str.count('nice'))  #1
12 print(str.count('nice',10,17))  #1
13 print(str.count('hi'))   #0
14 
15 print(str.rfind('nice'))  #13
16 print(str.rfind('nice',10,17))  #13
17 print(str.rfind('hi'))  #-1
18 
19 print(str.rindex('nice'))  #13
20 print(str.rindex('nice',10,17))  #13
21 #print(str.rindex('hi'))  # erroy

 3、修改操作

在python中修改操作一般常用的有:replace、split、join三个重要的方法。另外还有大小写转换、空白格删除、对其等非重要方法

replace(‘旧子串’,‘新子串’,替换次数)

  • 返回值:一个新的字符串
  • 以新替久

split(‘分割标记子串’,查找的次数)

  • 返回值:列表
  • 分隔开以后生成列表,并且将标记子串删除

分割字符串. join(列表)

  • 返回值:字符串
  • 将列表元素按照顺序用分割字符串连接起来
 1 str='hello python nice to meet you'
 2 
 3 newstr=str.replace('to','kkk',1)
 4 print(str)    # hello python nice to meet you
 5 print(newstr)  # hello python nice kkk meet you
 6 
 7 list=str.split('to')
 8 print(list)   #['hello python nice ', ' meet you']
 9 
10 List=['aa','bb','cc','dd']
11 NewStr=' and '.join(List)
12 print(NewStr)     #aa and bb and cc and dd
13 
14 mystr = "  hello world and itcast and itheima and Python  "
15 
16 # 1、capitalize() 字符串首字母大写
17 new_str = mystr.capitalize()
18 print(new_str)
19 # 2.title(): 字符串中每个单词首字母大写
20 new_str = mystr.title()
21 print(new_str)
22 # 3. upper():小写转大写
23 new_str = mystr.upper()
24 print(new_str)
25 # 4. lower(): 大写转小写
26 new_str = mystr.lower()
27 print(new_str)
28 
29 # 1. lstrip(): 删除左侧空白字符
30 new_str = mystr.lstrip()
31 print(new_str)
32 # 2. rstrip(): 删除右侧空白字符
33 new_str = mystr.rstrip()
34 print(new_str)
35 # 3.strip():删除两侧空白字符
36 new_str = mystr.strip()
37 print(new_str)
38 
39 '''  输出
40 hello world and itcast and itheima and python  
41   Hello World And Itcast And Itheima And Python  
42   HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON  
43   hello world and itcast and itheima and python  
44 hello world and itcast and itheima and Python  
45   hello world and itcast and itheima and Python
46 hello world and itcast and itheima and Python
47 
48 '''

 对其操作:

 1 >>> mystr='hello'
 2 >>> mystr.ljust(10)
 3 'hello     '
 4 >>> mystr.rjust(10)
 5 '     hello'
 6 >>> mystr.ljust(10,'.')
 7 'hello.....'
 8 >>> mystr.rjust(10,'.')
 9 '.....hello'
10 >>> mystr.center(10)
11 '  hello   '

 4、判断操作

在python中判断操作一般常用的有字母判断、数字判断、数字字母组合判断、空格判断。

 1 mystr = "hello world and itcast and itheima and Python"
 2 
 3 # 1. startswith(): 判断字符串是否以某个子串开头
 4 print(mystr.startswith('hello'))  #T
 5 print(mystr.startswith('hel'))    #T
 6 print(mystr.startswith('hels'))   #F
 7 
 8 
 9 # 2. endswith(): 判断字符串是否以某个子串结尾
10 print(mystr.endswith('Python'))   #T
11 print(mystr.endswith('Pythons'))  #F
12 
13 
14 # 3. isalpha(): 字母
15 print(mystr.isalpha())  #F
16 
17 # 4. isdigit(): 数字
18 print(mystr.isdigit())  #F
19 mystr1 = '12345'
20 print(mystr1.isdigit())  #T
21 
22 # 5. isalnum() : 数字或字母或组合
23 print(mystr1.isalnum())  #T
24 print(mystr.isalnum())   #F
25 mystr2 = 'abc123'
26 print(mystr2.isalnum())  #T
27 
28 
29 # 6.isspace(): 空白
30 print(mystr.isspace())  #F
31 mystr3 = '   '
32 print(mystr3.isspace())  #T

猜你喜欢

转载自www.cnblogs.com/lzy820260594/p/11789361.html