当前位置: 移动技术网 > IT编程>脚本编程>Python > Python基础之字符串

Python基础之字符串

2019年12月13日  | 移动技术网IT编程  | 我要评论

战地尖刀电视剧全集,洪亮的近义词是什么,1234 1234

1.创建字符串

  字符串用于存储和表示文本,在python中属于不可变对象,单引号、双引号、三引号内的内容即字符串。

1 s = '字符串'
2 s1 = "字符串"
3 s2 = '''字符串'''
4 print(s,s1,s2)
5 结果:
6 字符串 字符串 字符串

2.判断字符串

  isinstance(s,str)普通字符串;isinstance(s,unicode)unicode类型字符串;isinstance(s,basestring)basestring是str和unicode的基类。貌似py3.x中没有isinstance()无法判断unicode和basestring。

1 s = 'string'
2 print(isinstance(s,str))
3 结果:
4 true

3.性质判定

1)st.isalpha()判断是否全是字母。

1 st = 'abcdef123'
2 print(st.isalpha())
3 结果:
4 false

2)st.isalnum()判断是否全是数字和字母。

1 st = 'abcdef123'
2 st1 = 'abcde&f123'
3 print(st.isalnum())
4 print(st1.isalnum())
5 结果:
6 true
7 false

3)st.isdigit()判断是否全是数字。

1 st = 'abcdef123'
2 print(st.isdigit())
3 结果:
4 false

4)st.islower()判断字符串中字母是否都是小写。

1 st = 'abcdef123'
2 st1 = 'abcdef123'
3 print(st.islower())
4 print(st1.islower())
5 结果:
6 false
7 true

5)st.isupper()判断字符串中字母是否都是大写。

1 st = 'abcdef123'
2 st1 = 'abcdef123'
3 print(st.isupper())
4 print(st1.isupper())
5 结果:
6 false
7 true

6)st.isspace(0判断字符串中是否全是空白字符。

1 st = 'abcdef123'
2 st1 = '    '
3 print(st.isspace())
4 print(st1.isspace())
5 结果:
6 false
7 true

 7)st.istitle()判断字符串中是否是仅有首字母大写。

 1 st = 'abcdef123'
 2 st1 = 'abcdef123'
 3 st2 = 'abcdef123'
 4 print(st.istitle())
 5 print(st1.istitle())
 6 print(st2.istitle())
 7 结果:
 8 false
 9 false
10 true

8)st.startswith()判断字符串是否是以指定字符开头。

1 st = 'abcdef123'
2 st1 = 'abcdef123'
3 print(st.startswith('a'))
4 print(st1.startswith('a'))
5 结果:
6 true
7 false

9)st.endswith()判断字符串是否是以指定字符结尾。

1 st = 'abcdef123'
2 st1 = 'abcdef1234'
3 print(st.endswith('3'))
4 print(st1.endswith('3'))
5 结果:
6 true
7 false

4.查找与替换

1)st.count()查找字符串中某个字符的个数,所找字符后面还可以附加索引范围,查找指定范围内字符的个数。

1 st = 'ab2cdef1223'
2 print(st.count('2'))
3 print(st.count('2',0,5))
4 结果:
5 3
6 1

2)st.find()查找字符串中某个字符的索引值,不存在则返回-1,当存在多个时,返回从左往右第一个;st.rfind()与st.find()类似,但是从右往左查找。

1 st = 'ab2cdef1223'
2 print(st.find('de'))
3 print(st.find('ade'))
4 结果:
5 4
6 -1

3)st.index()与st.find()类似,但是所查找的字符不存在时,报错;st.rindex()与st.index()类似,但是从右往左查找,不存在时同样报错。

4)st.replace()用一个新的字符来替换字符串中的谋个子串,可自定替换的个数,不指定则全部替换。

1 st = 'ab2cdaef1a2a23'
2 print(st.replace('a','0'))
3 print(st.replace('a','0',1))
4 结果:
5 0b2cd0ef102023
6 0b2cdaef1a2a23

5.分切和链接

1)st.partition()用指定分隔符将字符串分割,如果分隔符存在字符串中,返回一个3个元素的元组,第一个元素为字符串中分隔符左边的子串,第二个元素为分隔符,第三个元素为字符串中分隔符右边的子串;如果分隔符不在字符串中,则返回(st,",");st.rpartition()与st.partition()类似,只是从右往左找。

1 st = 'abcdef123'
2 print(st.partition('d'))
3 print(st.partition('d1'))
4 结果:
5 ('abc', 'd', 'ef123')
6 ('abcdef123', '', '')

2)st.splitlines()按照('\n','\r','\r\n')分割,返回一个以各行作为元素的列表

1 st = 'ab\nc1\r\na23\na'
2 print(st)
3 print(st.splitlines())
4 结果:
5 ab
6 c1
7 a23
8 a
9 ['ab', 'c1', 'a23', 'a']

3)st.split()以指定字符作为分隔符,返回一个列表;st.rsplit()同st.split(),但是是从右边开始。

1 st = 'abcdef123'
2 st1 = 'abcdefc123'
3 print(st.split('c'))
4 print(st1.split('c'))
5 结果:
6 ['ab', 'def123']
7 ['ab', 'def', '123']

6.变形

1)st.lower()将字符串中所有大写字母全部转换为小写字母。

1 st = 'abcdef sdfgfg'
2 print(st.lower())
3 结果:
4 abcdef sdfgfg

2)st.upper将字符串中所有小写字母全部转换为大写字母。

1 st = 'abcdef sdfgfg'
2 print(st.upper())
3 结果:
4 abcdef sdfgfg

3)st.capitalize()将字符串中首字母大写,其余字母全部小写。

1 st = 'abcdef sdfgfg'
2 print(st.capitalize())
3 结果:
4 abcdef sdfgfg

4)st.swapcase()将字符串中大小写字母进行互换,即大写转小写,小写转大写。

1 st = 'abcdef sdfgfg'
2 print(st.swapcase())
3 结果:
4 abcdef sdfgfg

5)st.title()将字符串标题话,手游单词首字母大写,其余小写,无法去除字符串中首尾的空白符;string模块中的string.capword(st)可以去除首尾空白符以及单词间多余的空白符。

 1 1 st = 'abcdef sdfgfg'
 2 2 print(st.title())
 3 3 结果:
 4 4 abcdef sdfgfg
 5 
 6 *****分割线******
 7 
 8 import string
 9 st = '   adcdef   sdfgfg   '
10 print(st.title())
11 print(string.capwords(st))
12 结果:
13    adcdef   sdfgfg   
14 adcdef sdfgfg

7.删减与填充

1)st.strip()移除字符串两边空白字符,也可指定移除首尾字符;st.lstrip()移除左边空白符,也也移除指定首字符;st.rstrip()移除右边空白符,也可移除指定末尾字符。

 1 st = '   abcdef123    '
 2 print(st.strip())
 3 结果:
 4 abcdef123
 5 
 6 ****分割线*****
 7 
 8 st = 'abcdef123a'
 9 print(st.strip('a'))
10 结果:
11 bcdef123

2)st.center()将字符串居中,指定位数,指定补充符;st.ljust()将字符串左对齐,多余位数由补充符填充;st.rjust()将字符串右对齐,多余位数由补充符填充。

1 st = 'abcdef123a'
2 print(st.center(50,'*'))
3 print(st.ljust(50,'*'))
4 print(st.rjust(50,'*'))
5 结果:
6 ********************abcdef123a********************
7 abcdef123a****************************************
8 ****************************************abcdef123a

3)st.zfill()将字符串指定位数,少于指定位数时左对齐用0填充,多余指定位数时全部返回。

1 st = 'abcdef123a1111'
2 st1 = 'abcde'
3 print(st.zfill(10))
4 print(st1.zfill(10))
5 结果:
6 abcdef123a1111
7 00000abcde

8.字符串切片

  与列表相同,字符串由严格的顺序,可通过索引进行切片。

st = 'abc1a23a'
print(st[1:4:1])
结果:
bc1

9.敏感词汇过滤、去掉字符串两端的双引号

  str.maketrans()存放需过滤的词汇,st.translate()方法过滤

1 word_input = input('输入敏感词汇:')
2 sv = str.maketrans("苍井空",'***')
3 new_word = word_input.translate(sv)
4 print(new_word)
5 结果:
6 输入敏感词汇:勇敢的苍井空
7 勇敢的***

  去掉字符串内的双引号,eval()方法。

1 st = '"string"'
2 print(st)
3 print(eval(st))
4 结果:
5 "string"
6 string

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网