字符串 基本操作

字符串介绍:

 

1、strip 去除左右的空格

1 >>> a
2 ' bao '
3 >>> a.strip()
4 'bao'
strip

2、lstrip 去除左边的空格

1 >>> a
2 ' bao '
3 >>> a.lstrip()
4 'bao '
lstrip

3、rstrip 去除右边的空格

1 >>> a
2 ' bao '
3 >>> a.rstrip()
4 ' bao'
rstrip

4、复制字符串

1 >>> a
2 ' bao '
3 >>> b = a
4 >>> b
5 ' bao '
copy

5、字符串的拼接

1 >>> a
2 ' bao '
3 >>> b
4 ' bao '
5 >>> b +=a
6 >>> b
7 ' bao  bao '
8 >>>
拼接

6、查找字符 

1 >>> a
2 ' bao  bao '
3 >>> c = 'b'
4 >>> d = a.index(c)
5 >>> d
6 1
View Code

7、判断字符串是否包含子串的

1 >>> a = '12345678'
2 >>> b ='234'
3 >>> result = b in a
4 >>> result
5 False
6 >>> b = '234'
7 >>> result = b in a
8 >>> result
9 True
View Code

8、len()     计算字符串的长度

len

9、将字符串的大小写切换upper 小变大   lower  大变小

1 >>> a = 'TDrGdSe'
2 >>> b = a.upper()
3 >>> b
4 'TDRGDSE'
upper
1 >>> a = 'TDrGdSe'
2 >>> b = a.lower()
3 >>> b
4 'tdrgdse'
lower

10、追加指定长度的字符串

1 >>> a = 'TDrGdSe'
2 >>> b = '123456'
3 >>> n = 3
4 >>> a +=b[0:n]
5 >>> a
6 'TDrGdSe123'
7 >> a +=b[0:5]
8 >>> a
9 'TDrGdSe12312345'
追加

11、复制自定字符串的长度

1 >>> a
2 'TDrGdSe12312345'
3 >>> c = a[0:8]
4 >>> c
5 'TDrGdSe1'
View Code

12、将字符串的前n个替换成指定的字符串

1 >>> a
2 'TDrGdSe12312345'
3 >>> d = 'r'
4 >>> n = 3
5 >>> t = n * d + a[3:]
6 >>> t
7 'rrrGdSe12312345'
View Code

13、扫描字符串

 1 bao =  'ruguokeyi'
 2 zhang = 'dd'
 3 Npos = -1
 4 for i in bao:
 5     if i in zhang:
 6         Npos = bao.index(i)
 7         break
 8 print(Npos)
 9 
10 0  表示存在
11 -1 表示不存在
View Code

14、字符串反转

1 >>> t
2 'rrrGdSe12312345'
3 >>> t = t[::-1]
4 >>> t
5 '54321321eSdGrrr'
View Code

15、查找字符串

1 >>> t
2 '54321321eSdGrrr'
3 >>> z = '543'
4 >>> t.find(z)
5 0
View Code

16、字符串分割

 1 str = ’0123456789 2 print str[0:3] #截取第一位到第三位的字符
 3 print str[:] #截取字符串的全部字符
 4 print str[6:] #截取第七个字符到结尾
 5 print str[:-3] #截取从头开始到倒数第三个字符之前
 6 print str[2] #截取第三个字符
 7 print str[-1] #截取倒数第一个字符
 8 print str[::-1] #创造一个与原字符串顺序相反的字符串
 9 print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符
10 print str[-3:] #截取倒数第三位到结尾
11 print str[:-5:-3] #逆序截取,具体啥意思没搞明白
View Code

 17、title 标题 首字母大写  只有首字母大写,其余为小写,模块中没有这个方法 

1 >>> bao = "beijing"
2 >>> bao.title()
3 'Beijing'
title

18、join  把列表变成字符串

1 >>> bao = ['bei','jing']
2 >>> '|'.join(bao)
3 'bei|jing'
join

19、split 以什么为分隔并且去掉,把字符串变成列表 

1 >>> bao = "bao|jin|qiang"
2 >>> bao.split("|")
3 ['bao', 'jin', 'qiang']
split

20、center 以什么为中心

1 >>> bao = "bao|jin|qiang"
2 >>> bao.center(30,"-")
3 '--------bao|jin|qiang---------'
center

21、以左侧对齐 ljust

1 >>> bao = "bao|jin|qiang"
2 >>> bao.ljust(50,"-")
3 'bao|jin|qiang-------------------------------------'
View Code

22、以右侧对齐

1 >>> bao = "bao|jin|qiang"
2 >>> bao.rjust(50,"-")
3 '-------------------------------------bao|jin|qiang'
rjust

 23、format

1 >>> bao = "I im {name} my year {old}"
2 >>> bao.format(name="baojinqiang",old="30")
3 'I im baojinqiang my year 30'
View Code

24、isalnum 包含所有的英文字符和1-9就为真

1 >>> 'bao123'.isalnum()
2 True
View Code
1 >>> 'bao123'.isalnum()
2 True
3 >>> 'bao#123'.isalnum()
4 False
5 >>> 'bao'.isalnum()
6 True
7 >>> '123'.isalnum()
8 True
View Code

25、isidentifier  判断是不是一个合法的标识符(变量名)

1 >>> 'a'.isidentifier()
2 True
3 >>> 'a333'.isidentifier()
4 True
5 >>> 'a3_33'.isidentifier()
6 True
7 >>> 'a3_3#3'.isidentifier()
8 False
9 >>>
View Code

 

posted @ 2017-05-02 18:55  Sober--Never  阅读(90)  评论(0)    收藏  举报