Python 基础 —— 字符串 方法

python 版本为 3.5.2

1,capitalize()

  将字符串的首字母变为大写,后面的字母全部小写

1 >>> test = 'cong'   
2 >>> v1 = test.capitalize()
3 >>> print(v1)
4 Cong
5 >>> test2 = 'CONG'          
6 >>> v2 = test2.capitalize()
7 >>> print(v2)              
8 Cong
9 >>> 

2,casefold()

  将字符串所有的字母变为小写

1 >>> test1 = 'Cong'
2 >>> v1 = test1.casefold()
3 >>> print(v1)
4 cong
5 >>> test2 = 'cOnG'
6 >>> v2 = test2.casefold()
7 >>> print(v2)
8 cong
9 >>> 

3,lower()

  这个也是讲所有字母变为小写,但是有一些特殊的大小写转换这个是转换不了的。

1 >>> test1 = 'Cong'
2 >>> v1 =test1.lower()
3 >>> print(v1)
4 cong
5 >>>

4,center()

  center(self, width, fillchar=None)

  设置多少个字符的宽度,包括字符串内容在内的宽度字符,并将字符串内容居中,可以设置一个字符来填充,默认是空白字符

  width 表示多少个宽度字符,fillchar表示以什么字符来填充除字符串内容以外的字符位置。默认是空白

1 >>> test1 = 'Cong'
2 >>> v1 = test1.center(10)
3 >>> print(v1)
4    Cong   
5 >>> test2 = 'cong'
6 >>> v2 = test2.center(10, '*')
7 >>> print(v2)
8 ***cong***
9 >>>

5,count()

  count(self, sub, start=None, end=None)

  统计指定的字符或字符串在变量值中的个数

  sub 表示要统计的字符或字符串

  start 表示统计的起始位置,可选

  end 表示统计的结束位置,可选

 1 >>> test1 = 'linyicong'
 2 >>> v1 = test1.count('i')
 3 >>> print(v1)
 4 2
 5 >>> test2 = 'the is test'
 6 >>> v2 = test2.count('t')
 7 >>> print(v2)
 8 3
 9 >>> test3 = 'the is test test test'
10 >>> v3 = test3.count('est')
11 >>> print(v3)
12 3
13 >>> 

6,endswith()

  endswith(self, suffix, start=None, end=None)

  判断变量的值是否以指定的字符或字符串结尾,如果是则返回True,否则返回False。

  suffix 表示需要判断以该字符或字符串结尾的

  start 检查的起始位置,可选

  end 检查的结束位置,可选

1 >>> test3 = 'the is test test test'
2 >>> v3 = test3.endswith('est')
3 >>> print(v3)
4 True
5 >>> vv3 = test3.endswith('e') 
6 >>> print(vv3)
7 False
8 >>> 

7,startswith()

   startswith(self, prefix, start=None, end=None)

  判断变量的值是否以指定的字符或字符串开头的。是则返回True。否则返回False

1 >>> test1 = 'test and test'
2 >>> v1 = test1.startswith('tes')
3 >>> print(v1)
4 True
5 >>> test2 = 'test and test'
6 >>> v2 = test2.startswith('est')
7 >>> print(v2)
8 False
9 >>> 

8,find()

  find(self, sub, start=None, end=None)

  在变量的值中,查找指定的字符或字符串。返回第一个字符或字符串的位置,位置从0开始算起,

  如果没有找到指定的字符或字符串则返回-1,

  可以指定查找的起始位置和结束位置,结束位置的范围是小于设置的位置数。比如设置结束位置是7,那么第7个字符是不在查找范围内的。只查找到第6个字符。

 1 >>> test1 = 'test and dev'
 2 >>> v1 = test1.find('an')
 3 >>> print(v1)
 4 5
 5 >>> vv1 = test1.find('u')
 6 >>> print(vv1)
 7 -1
 8 >>> vvv1 = test1.find('d', 0, 6)
 9 >>> print(vvv1)
10 -1
11 >>> vvvv1 = test1.find('d', 0, 7)
12 >>> print(vvvv1)
13 -1
14 >>> vvvv1 = test1.find('d', 0, 8)
15 >>> print(vvvv1)                 
16 7
17 >>>

9,format()

  format(self, *args, **kwargs)

  格式化,将一个字符串中的占位符替换为指定的内容,占位符只能由{} 符号括起来表示,其他符号无效。

 1 >>> test1 = 'my name is {n1}' 
 2 >>> v1 = test1.format(n1='cong')
 3 >>> print(v1)
 4 my name is cong
 5 >>> test2 = 'my name is {n1}, age is {a}' 
 6 >>> v2 = test2.format(a=18, n1='cong')    # 可以以变量复制的方式替换指定内容
 7 >>> print(v2)
 8 my name is cong, age is 18
 9 >>> test3 = 'my name is {0}, age is {1}'   # 也可以是按照参数位置来指定替换的内容
10 >>> v3 = test3.format('cong', 18)
11 >>> print(v3)
12 my name is cong, age is 18
13 >>> 

10,format_map

  format_map(self, mapping)

  格式化,将一个字符串中的占位符替换为指定的内容,但是格式要以key:value 一对的填写,类似于字典的方式,

1 >>> test1 = 'my name is {n}, age is {a}'        
2 >>> v1 = test1.format_map({"n":'cong', 'a':18})
3 >>> print(v1)
4 my name is cong, age is 18
5 >>> 

11,index()

  index(self, sub, start=None, end=None)

  在变量中查找指定的字符或字符串,功能类似是find方法。

  但是find方法没找到字符或字符串时,是返回 -1,而index方法找不到字符或字符串时直接报错了。

1 >>> test1 = 'my name is cong'                  
2 >>> v1 = test1.index('co')
3 >>> print(v1)
4 11
5 >>> vv1 = test1.index('qq')
6 Traceback (most recent call last):
7   File "<stdin>", line 1, in <module>
8 ValueError: substring not found
9 >>> 

12,isalnum()

  判断变量的值中是否只有字母,或数字,(有中文好像也可以),如果是则返回True,否则返回False

 1 >>> test1 = 'test11'
 2 >>> v1 = test1.isalnum()
 3 >>> print(v1)
 4 True
 5 >>> test2 = 'test11++'
 6 >>> v2 = test2.isalnum()
 7 >>> print(v2)
 8 False
 9 >>> test3 = 'test'
10 >>> v3 = test3.isalnum()
11 >>> print(v3)
12 True
13 >>> test4 = '中午'
14 >>> v4 = test4.isalnum()
15 >>> print(v4)
16 True
17 >>>
原文地址:https://www.cnblogs.com/LYCong/p/8834997.html