python string

通过例子讲解python是最好的办法,利于学习,利于记忆,做好笔记

string.replace(str, old, new[, maxreplace])
import string
s='123456789123'
print s
#替换算法 string.replace(s, old, new, maxreplace) print string.replace(s, '123', '*****') print string.replace(s, '123', '*****',1)
print s.replace('123',"fuck")

#输出如下:
#123456789123
#*****456789*****
#*****456789123
#fuck456789fuck
string.zfill(s, width)
import string
print string.zfill("12345", 6)
print string.zfill("-1234", 6)
print string.zfill("123456", 6)

 输出:

012345
-01234
123456

string.strip(s[, chars])
import string
print string.strip('   hello world   ')
print string.lstrip('   hello world   ')
print string.rstrip('   hello world   ')
print string.strip('***hello world***','*')
print string.strip('hello world','world')

输出:

In [1]: import string

In [2]: string.strip(' hello world ')
Out[2]: 'hello world'

In [3]: string.strip(' hello world','world')
Out[3]: ' hello '

In [4]: string.strip('   hello world   ')
Out[4]: 'hello world'

In [5]: string.lstrip('   hello world   ')
Out[5]: 'hello world   '

In [6]: string.rstrip('   hello world   ')
Out[6]: '   hello world'

In [7]: string.strip('***hello world***','*')
Out[7]: 'hello world'

In [8]: print string.strip('hello world','world')
hello

原文地址:https://www.cnblogs.com/UnGeek/p/2990946.html