字符串,和列表、元组,都适用序列操作。
关于python转义字符
迭代
for x in S: print(x)
[c * 2 for c in S]
Comparasion:
>>> title = "mean"'of'"lid"
>>> title
'meanoflid'
>>> title = "mean",'of',"lid"
>>> title
('mean', 'of', 'lid')
python3中如何print不换行:
>>> myjob = "hacker"
>>> for c in myjob: print(c, end=' ')
...
h a c k e r >>>
索引和分片:通过[]进行操作
扩展分片:s[x:y:z],索引s中的元素,从偏移x到偏移为y-1,每隔z个元素索引一次
>>> s = 'abcdefghijklmnopqrstuvwxyz'
>>> s[1:20:2]
'bdfhjlnprt'
>>> s = 'hello'
>>> s[::-1]
'olleh'
>>> s = 'abcedfg'
>>> s[5:1:-1]
'fdec'
sys中的argv属性: non-understanding
chr() & ord() #ASCII码与字符转换
str() & int() or float() #数转串 & 串转数
内置eval函数: non-understanding
一些方法调用:
( 它们都不会改变字符串s )
'X' in S
s.find('pa') # 返回字串位置,若无返回-1
s.rstrip() # 移除末尾字符(默认是空格)
s.replace('a', 'b') # replace all
s.replace('a', 'b', k) # replace k times
s.split([',',[2]]) # be split by ',' for 2 times
s.isdigit() # 或是isalpha
s.lower() # 或是upper
s.endswith('spam') # 或是startswith
'spam'.join(s) # 用‘spam’将s所有字符连接
'X'.join([S1, S2, S3]) # 用‘X’将S1, S2, S3连接
关于 rstrip
若需要对一个超长字符串进行许多的修改,为了优化脚本的性能,可能需要将字符串转换为一个支持原处修改的对象。例如:list函数
>>> S = 'spammy'
>>> L = list(S)
>>> L[3] = 'x'
>>> S = ''.join(L)
格式化表达式:
"%a %s parrot" % kind
"a {0} parrot".format(kind)
'That is %d %s bird!' % (1, 'dead')
'That is {0} {1} bird!'.format(1, 'dead')
'%s -- %s -- %s' % (42, 3,14159, [1, 2, 3])
#所有类型都可以转换为字符串,故都用%s也是正确的
'%.*f' % (4, 1/3.0)
#用*指定width和precision,它们的值从%运算符右边的下一项获取
details : http://www.cnblogs.com/vamei/archive/2013/03/12/2954938.html
基于字典的字符串格式化
>>> "%(n)d %(x)s" % {"n":1, "x":"spam"}
'1 spam'
>>> reply = """
Greetings...
Hello %(name)s!
Your age squared is %(age)s
"""
>>> values = {'name': 'Bob', 'age': 40}
>>>print(reply % values)
内置函数vars:
>>> food = 'spam'
>>> age = 40
>>> vars()
{'food':'spam', 'age':40, ...many more...}
>>> "%(age)d %(food)s" % vars()
'40 spam'
在主体字符串中,花括号通过位置({1})或关键字({food})指出替换目标及将要插入的参数。
>>> template = '{0}, {1} and {2}'
>>> template.format('spam', 'ham', 'eggs')
'spam, ham and eggs'
>>> template = '{motto}, {pork} and {food}'
>>> template.format(motto='spam', pork='ham', food='eggs')
'spam, ham and eggs'
>>> '{motto}, {0} and {food}'.format(42, motto=3.14, food=[1, 2])
'3.14, 42 and [1, 2]'
格式化字符串可以指定对象属性和字典键,方括号指定字典键,点表示位置或关键字所引用的一项的对象属性。
>>> import sys
>>> 'My {1[spam]} runs {0.platform}'.format(sys, {'spam':'laptop'})
'My laptop runs darwin'
>>> 'My {config[spam]} runs {sys.platform}'.format(sys=sys,
config={'spam':'laprop'})
'My laptop runs darwin'
方括号可以指定列表(及其他的序列)偏移量以执行索引。
>>> somelist = list('spam')
>>> 'first = {0[0]}, third = {0[2]}'.format(somelist)
'first = s, third = a'
>>> 'first = {0}, last = {1}'.format(somelist[0], somelist[-1])
'first = s, last = m'
>>> parts = somelist[0], somelist[-1], somelist[1:3]
>>> 'first = {0}, last = {1}, middle = {2}'.format(*parts) ##
"first = s, last = m, middle = ['p', 'a']"
??parts前为什么要有 ' * ' , 是什么意思
添加具体格式化
{fieldname!conversionflag:formatspec}
fieldname: 指定参数的一个数字或关键字,后面跟着可选的“.name”或“[index]”成分引用
conversionflag: 对repr、str、ascii内置函数的调用,用r、s、a表示
formatspec: 包括字段宽度、对齐方式、补零、小数点精度等细节
>>> '{0:>10} = {1:<10}'.format('spam', 123.4567)
' spam = 123.4567 '
>>> '{0.platform:>10} = {1[item]:<10'.format(sys, dict(item='laptop'))
' darwin = laptop '
>>> '{0:e}, {1:.3e}, {2:g}'.format(3.14159, 3.14159, 3.14159)
'3.141590e+00, 3.142e+00, 3.14159'
>>> '{0:f}, {1:.2f}, {2:06.2f}'.format(3.14159, 3.14159, 3.14159)
'3.141590, 3.14, 003.14'
>>> '{0:X}, {1:o}, {2:b}'.format(255, 255, 255)
'FF, 377, 111111111'
>>> '{0:.{1}f}'.format(1 / 3.0, 4)
'0.3333'
>>> '%.*f' % (4, 1 / 3.0)
'0.3333'
>>> format(1.2345, '.2f')
'1.23'