Beginning Python - Chapter3 : Strings

------------chapter3: Strings

----------basic string operations
all sequences' operations ; but cannot be changed

----------string formatting:the short version
--------generic formatting
Note:If you use a list or some other sequence instead of a tuple, the sequence will be interpreted as a single
value. Only tuples and dictionaries (discussed in Chapter 4) will allow you to format more than one value.
>>> format = "hello,%s %s"
>>> value = ('girl','boy')
>>> print format %value
hello,girl boy

>>> format = "hello,% %"
>>> print format % value
wrong

note:%%
>>> format ="hello,%% %s"
>>> v='girl'
>>> print format % v
hello,% girl

note:other data type & numbers of decimals you want to keep
>>> format = "thin num is %.2f"
>>> from math import pi
>>> print format %pi
thin num is 3.14
>>> pi
3.141592653589793

--------template strings
>>> from string import Template
>>> s=Template("$x,again,$x!")
>>> s.substitute(x='girl')
'girl,again,girl!'

note:$$
note:Instead of using keyword arguments, you can supply the value-name pairs in a dictionary (see Chapter 4):
>>> s=Template("$x,again,$x!")
>>> s.substitute(x='girl')
'girl,again,girl!'
>>> s=Template('$x have $y $$')
>>> d={}
#dictionary
>>> d['x']='i'
>>> d['y']='30'
>>> s.substitute(d)
'i have 30 $'

----------String module??? Question p87
----------String methods
--------find
note:
that the range specified by the start and stop values (second and third parameters)
includes the first index but not the second. This is common practice in Python.
In Appendix B: rfind, index, rindex, count, startswith, endswith.
>>> '0 234 67 67 8'.find('234')
2
>>> '0 234 67 67 8'.find('6')
6
>>> '0 234 67 67 8'.find('6',7)
9
>>> '0 234 67 67 8'.find('8',10,11)
-1
# Question:why cannot find ,still be -1 ??
>>> '0 234 67 67 8'.find('8',0,9)
-1

--------join
note:just string ;See also: split.
>>> s=['1','2','3']
>>> t='+'
>>> t.join(s)
'1+2+3'

>>> dir = '','user','bin'
>>> '/'.join(dir)
'/user/bin'

--------split
note:the inverse of join
   See also: join.
     In Appendix B: rsplit, splitlines.

>>> '1+2+3'.split('+')
['1', '2', '3']
>>> "user/bin/dev".split("/")
['user', 'bin', 'dev']
>>> 'using the default'.split()
['using', 'the', 'default']

--------lower
note:
In Appendix B: islower, capitalize, swapcase, title, istitle, upper, isupper.
>>> 'Hello'.lower()
'hello'
--------title casing
>>> "that'a all folks".title()
"That'A All Folks"
#string module
>>> import string
>>> string.capwords("that's all folks")
"That's All Folks"

--------replace
note:
See also: translate.
In Appendix B: expandtabs.
>>> 'this is a test'.replace('is','are')
'thare are a test'

--------translate
note:
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.

>>> from string import maketrans
>>> table=maketrans('cs','kz')
>>> len(table)
256
>>> table[97:123]
'abkdefghijklmnopqrztuvwxyz'
>>> 'this is an test'.translate(table)
'thiz iz an tezt'

--------strip
note:
Stripping is performed only at the ends, so the internal asterisks are not removed.
In Appendix B: lstrip, rstrip.

>>> ' internal whitespace is kept '.strip()
'internal whitespace is kept'

>>> ' *!test**test** '.strip(' !*')
'test**test'

#Question:why?
>>> ' *!test**test** '.strip('*!')
' *!test**test** '

 

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