str = 'hk$$yicunyiye$$hello world'
print(str.split('$$'))
#自己实现
result = ''
for i in str.split('$$'):
result += i+' '
print(result)
#内置函数
print(','.join(str.split('$$')))
输出
['hk', 'yicunyiye', 'hello world']
hk yicunyiye hello world
hk,yicunyiye,hello world
str = ['a.py','b.exe','c.e','d.erl']
for s in str:
if s.endswith('.py'):
print(s)
输出a.py
str = ['a.py','b.exe','c.e','d.erl']
for s in str:
if s.startswith('a'):
print(s)
输出a.py
intab = 'aeiou'
outtab = '12345'
trantab = str.maketrans(intab,outtab)
result = "this is string example...wow!!"
print(result.translate(trantab))
输出
th3s 3s str3ng 2x1mpl2…w4w!!
#单次替换
print(result.replace('i','1').replace('e','2'))
输出
th1s 1s str1ng 2xampl2…wow!!
string = ' yicunyiye '
print(string.strip())
//类似于
print(string.lstrip())
print(string.rstrip())
name='yicunyiye'
age=20
string = 'name is {}; age is {};'.format(name,age)
print(string)
and
name='yicunyiye'
age=20
string = f'name is {name}; age is {age};'
print(string)
输出
name is yicunyiye; age is 20;
手机扫一扫
移动阅读更方便
你可能感兴趣的文章