strip , replace,just(rjust,ljust
2019-05-07 本文已影响0人
LittleBear_6c91
strip() 方法在读取和清理数据以备后续处理的时候是经常会被用到的。比
如,你可以用它们来去掉空格,引号和完成其他任务。
但是需要注意的是去除操作不会对字符串的中间的文本产生任何影响。

如果你想处理中间的空格,那么你需要求助其他技术。比如使用 replace() 方法
或者是用正则表达式替换

一般来讲,代码越简单运行越
快。对于简单的替换操作,str.replace() 方法通常是最快的,甚至在你需要多次调用
的时候。比如,为了清理空白字符,你可以这样做:

对于基本的字符串对齐操作,可以使用字符串的 ljust() , rjust() 和 center()
方法。比如:

函数 format() 同样可以用来很容易的对齐字符串。你要做的就是使用 <,> 或者 ^
字符后面紧跟一个指定的宽度。比如:

format() 函数的一个好处是它不仅适用于字符串。它可以用来格式化任何值,使
得它非常的通用。比如,你可以用它来格式化数字:
