Beginning Python chapter 3: Working with strings

目錄

Formatting

join

replace

strip

translate

A Quick Summary


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).

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章