赞
踩
endswith
判断字符串是否以某个子串结尾a = 'spider.py'
a = a.endswith('.py')
True
startswith
判断字符串是否以某个子串开头a = 'http://www.baidu.com'
a = a.startswith('http:')
True
import os
li = [name for name in os.listdir('.') if name.endswith(('.sh', '.py'))]
print(li)
capitalize()
字符串首字母大写a = 'life is short , you need Python '
a.capitalize()
Life is short , you need python
title()
字符串中每个单词首字母大写a = 'life is short , you need Python '
a.title()
Life Is Short , You Need Python
upper()
小写转大写a = 'abcedfg'
a = a.upper()
ABCEDFG
lower()
大写转小写a = 'aBeDfG'
a = a.lower()
abcdef
isalpha()
判断是否字母
a = 'abc'
a.isalpha()
True
isdigit()
判断是否数字
a = '12345'
a.isdigit()
True
isalnum()
判断数字或字母或组合
a = 'abc123'
a.isalnum()
True
isspace()
判断空白
a = ' '
a.isspace()
True
split
分割,返回一个列表, 丢失分割字符
a = "abc;def;ghi"
a = a.split(';')
['abc', 'def', 'ghi']
splitlines() 按照行(’\r’, ‘\r\n’, \n’)分隔,返回一个包含各行作为元素的列表
如果参数 keepends 为 False,不包含换行符
如果为 True,则保留换行符。
sql = """SELECT
*
FROM
TABLE a
INNER JOIN TABLE b ON a.id = b.id;
"""
sql2 = sql.splitlines(True)
['SELECT\n', '\t* \n', 'FROM\n', '\tTABLE a\n', '\tINNER JOIN TABLE b ON a.id = b.id;\n']
sql = sql.splitlines(False)
['SELECT', '\t* ', 'FROM', '\tTABLE a', '\tINNER JOIN TABLE b ON a.id = b.id;']
# 将MySQL注释去掉,并转为小写
sql = """
-- MySQL单行注释方法
select * from sys_user limit 2;
"""
sql = ''.join(map(lambda x: re.compile(r'(^--\s+.*|^/\*.*\*/;\s*$)').sub('', x, count=1), sql.splitlines(1))).strip().lower()
select * from sys_user limit 2;
replace
替换,replace('旧字, ‘新字’)
a = '人生几河,我用python'
a = a.replace('河', '何')
人生几何,我用python
removeprefix
删除前缀 (仅删除第一个前缀)
'BaseTestCase'.removeprefix('Base') TestCase
'BaseBaseTestCase'.removeprefix('Base') BaseTestCase
removesuffix
删除后缀 (仅删除最后一个前缀)
'BaseTestCase'.removesuffix('Case') BaseTest
'BaseTestCaseCase'.removesuffix('Case') BaseTestCase
strip()
删除两侧空白字符a = ' abcdef '.
a = a.strip()
abcdef
lstrip()
删除左侧空白字符a = ' abcdef'.
a = a.lstrip()
abcdef
rstrip()
删除右侧空白字符a = 'abcdef '.
a = a.rstrip()
abcdef
ljust()
文本字符串左边对齐(可接受填充字符)
a = 'Life is short,you need Python'
a = a.ljust(20)
rjust()
文本字符串右边对齐(可接受填充字符)
a = 'Life is short,you need Python'
a = a.rjust(50)
a = a.rjust(50, '>')
rjust()
文本字符串居中对齐(可接受填充字符)
a = 'Life is short,you need Python'
a = a.center(50, '>')
简单应用
dic = {
'aa': 111,
"bbb": 333333,
"ddddddd": 2222222222,
"e": "4"
}
w = max(map(len, dic.keys()))
for k in dic:
print(k.ljust(w), ":", dic[k])
aa : 111
bbb : 333333
ddddddd : 2222222222
e : 4
join()
合并列表里面的字符串数据为一个大字符串(可接受填充字符)
a = ['Life', 'is', 'short', 'you', 'need', 'Python']
a = ''.join(a)
LifeisshortyouneedPython
a = ['Life', 'is', 'short', 'you', 'need', 'Python']
a = ' '.join(a)
Life is short you need Python
format()
字符串合并
a = 'Life is {} , you need{} '.format('short', 'Python')
Life is short , you needPython
f
字符串合并
m = 'short'
n = 'Python'
a = f'Life is {m} , you need {n} '
Life is short , you need Python
%
字符串合并 %s 字符串, %d数字,%f浮点
a = 'Life is %s , you need %s ' % ('short', 'Python')
Life is short , you need Python
a = 'Life is short , you need Python '
find()
存在是否存在,存在则返回下标, 不存在-1a.find('is')
5
.rfind()
a.rfind('e')
22 从有右侧开始
.index()
获取元素的下标,存在返回下标, 不存在保存a.index('is')
a.index('is',15,20)
.rindex()
a.rindex('is')
a.rindex('is',15,20)
count()
出现的频率a.count('s')
2 s出现的次数
字符串[开始索引:结束索引:步长]
切取字符串
为开始索引到结束索引-1内的字符串
步长
不指定时步长为1 字符串[开始索引:结束索引]
a = 'abcdefghijqmn'
下标即索引
a[0] 索引为零的元素 a
截取2 - 6位置的字符(前包含,后不包含)
a[2:6]
cdef
开始索引可以省略不写
a[:6]
abcdef 0-6之间字符串
结束索引可以省略不写
a[2:]
cdefghijqmn 2-结束的字符
完整字符串
a[:]
abcdefghijqmn
开始负数,无结束
a[-2:]
mn 从倒数第几个至结束
开始正数,结束为负数
a[2:-2]
cdefghijq 为两个数之间
开始负数数,结束为负数
a[-5:-2]
ijq 两个负数之间
从开始位置,每隔一个字符截取字符串
a[::2]
acegiqn
从索引1开始,每隔一个取一个
a[1::2]
bdfhjm
字符串的逆序
a[::-1]
nmqjihgfedcba
字符串的逆序,隔一个取一个
a[::-2]
nqigeca
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。