Beginning Python chapter 3: Working with strings
目录
Formatting
format = "Hello, %s. %s enough for ya?"
place = ('wold', 'china')
format % place
Out[6]: 'Hello, wold. china enough for ya?'
"{0}, {1} and {2}".format("first", "second", "third")
Out[11]: 'first, second and third'
The indices need not be in order like this, though.
>>> "{3} {0} {2} {1} {3} {0}".format("be", "not", "or", "to")
'to be or not to be'
strs = "{0}, {1:.5f}, {2}" .format(1, 2/3, 'poem')
strs
Out[8]: '1, 0.66667, poem'
join
A very important string method, join is the inverse of split. It is used to join the elements of a sequence.
As you can see, the sequence elements that are to be joined must all be strings.
"I love My country, China!".join("GREAT")
Out[10]: 'GI love My country, China!RI love My country, China!EI love My country, China!AI love My country, China!T'
replace
The replace method returns a string where all the occurrences of one string have been replaced by another.
goodWords = "None of us is perfect forever"
goodWords.replace('p', 'water')
Out[12]: 'None of us is watererfect forever'
goodWords.replace('perfect', 'hope')
Out[13]: 'None of us is hope forever'
strip
The strip method returns a string where whitespace on the left and right (but not internally) has been stripped (removed)
' internal whitespace is kept '.strip()
Out[15]: 'internal whitespace is kept'
'internal whitespace is kept '.strip('i')
Out[18]: 'nternal whitespace is kept '
lstrip, rstrip
translate
Similar to replace, translate replaces parts of a string, but unlike replace, translate works only with
single characters. Its strength lies in that it can perform several replacements simultaneously and can do so
more efficiently than replace.
To do this, you must replace the character c with k, and s with z.
Before you can use translate, however, you must make a translation table. This translation table
contains information about which Unicode code points should be translated to which. You construct such a
table using the maketrans method on the string type str itself. The method takes two arguments: two strings
of equal length, where each character in the first string should be replaced by the character in the same
position in the second string.
table = str.maketrans('cs', 'kz')
'this is an incredible test'.translate(table)
Out[21]: 'thiz iz an inkredible tezt'
A Quick Summary
In this chapter, you saw two important ways of working with strings.
String formatting: The modulo operator (%) can be used to splice values into a string that contains conversion flags, such as %s. You can use this to format values
in many ways, including right or left justification, setting a specific field width and precision, adding a sign (plus or minus), or left-padding with zeros.String methods: Strings have a plethora of methods. Some of them are extremely useful (such as split and join), while others are used less often (such as istitle or capitalize).
相关文章
- python爬虫知识点总结(二十七)Scrapy分布式原理以及Scrapy-Reids源码解析
- python中flask_restful响应处理(marshal、marshal_with、fields、reqparse、representation)
- python中with语句和上下文管理器
- Python的上下文管理语句“with语句和try/finally语句“是什么意思?
- python 微信跳一跳进阶
- Python filter() 函数
- python 之html的headers提取操作
- 《NLTK基础教程——用NLTK和Python库构建机器学习应用》——1.2 先从Python开始吧
- 用Python实现邮件发送Hive明细数据
- Python with/as和contextlib上下文管理使用说明
- 基于 Python 官方 GitHub 构建 Python 文档
- 分享某Python下的mpi教程 —— A Python Introduction to Parallel Programming with MPI 1.0.2 documentation ( 续 #2 )
- 36、【排序】快速排序实现(C/C++版+Python版本)
- 【零基础高效率学Python】第四天 怎么使用函数
- 【LeetCode】【Python题解】Reverse Integer
- python调参神器hyperopt
- python第三课——数据类型2
- python安装pyhash遇到的问题,pip install pyhash,ERROR: Command errored out with exit status 1