python之字符串函数

1.  endswith()  startswith()

1 # 以什么什么结尾
2 # 以什么什么开始
3 test = "alex"
4 v = test.endswith('ex')
5 v = test.startswith('ex')
6 print(v)
View Code

2. expandtabs()

1 test = "1\t2345678\t9"
2 v = test.expandtabs(6)
3 print(v,len(v))
View Code

3. find()

1 # 从开始往后找,找到第一个之后,获取其未知
2 # > 或 >=
3 test = "alexalex"
4 # 未找到 -1
5 v = test.find('e')
6 print(v)
View Code

4.  index()

1 # index找不到,报错   忽略
2 test = "alexalex"
3 v = test.index('a')
4 print(v)
View Code

5. format()  format_map()

 1 # 格式化,将一个字符串中的占位符替换为指定的值
 2 test = 'i am {name}, age {a}'
 3 print(test)
 4 v = test.format(name='alex',a=19)
 5 print(v)
 6 
 7 test = 'i am {0}, age {1}'
 8 print(test)
 9 v = test.format('alex',19)
10 print(v)
11 
12 # 格式化,传入的值 {"name": 'alex', "a": 19}
13 test = 'i am {name}, age {a}'
14 v1 = test.format(name='df',a=10)
15 print(v1)
16 v2 = test.format_map({"name": 'alex', "a": 19})
17 print(v2)
View Code

6. isalnum()

1 # 字符串中是否只包含 字母和数字
2 test = "er;"
3 v = test.isalnum()
4 print(v)
View Code

猜你喜欢

转载自www.cnblogs.com/sqy-yyr/p/9329178.html