您的位置:首页 > 编程语言 > Python开发

python 进阶学习之10

2014-07-22 11:34 387 查看

运行时刻字符串连接

>>> '%s %s' % ('spa','oil')

'spa oil'

>>> s=' ' .join(('ss','ww','21'))

>>> s

'ss ww 21'

编译时字符串连接

>>> foo='hello'"world"

>>> foo

'helloworld'

通过这种方法,你可以把长的字符串分成几部分来写,而不用加反斜杠

普通字符串转化为Unicode 字符串

如果把一个普通字符串和一个Unicode 字符串做连接处理,Python 会在连接操作前先把普通字符串转化为Unicode 字符串

>>> 'he'+u' '+'won'+u'!'

u'he won!'

只适用于字符串的操作符

格式化操作符( % )

调试工具 repr(),str()或``


字符串模板

Template 对象有两个方法,substitute()和safe_substitute().前者更为严谨,在key 缺少的情况下它会报一个KeyError 的异常出来,而后者在缺少key 时,直接原封不动的把字符串显示出来.

>>> from string import Template

>>> s=Template('are ${many}${lang}symbols')

>>> print s.substitute(lang='Python',many=3)

are 3Pythonsymbols

>>> print s.substitute(lang='Python')

Traceback (most recent call last):

File "<stdin>", line 1, in ?

File "/usr/lib64/python2.4/string.py", line 172, in substitute

return self.pattern.sub(convert, self.template)

File "/usr/lib64/python2.4/string.py", line 162, in convert

val = mapping[named]

KeyError: 'many'

>>> print s.safe_substitute(lang='Python')

are ${many}Pythonsymbols

原始字符串操作符( r/R )

在原始字符串里,所有的字符都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。

这个'r'可以是小写也可以是大写,唯一的要求是必须紧靠在第一个引号前.

>>> '\n'

'\n'

>>> print '\n'

>>> r'\n'

'\\n'

>>> print r'\n'

\n

Unicode 字符串操作符( u/U )

>>> u'abc'

u'abc'

内建函数

cmp()

序列类型函数


len()


max() and min()


enumerate()


>>> s='fool'

>>> for i,t in enumerate(s):

... print i,t

...

0 f

1 o

2 o

3 l

zip()


>>> s,t = 'foa','wer'

>>> zip(s,t)

[('f', 'w'), ('o', 'e'), ('a', 'r')]

字符串类型函数


raw_input()


str() and unicode()


chr(), unichr(), and ord()


字符串不变性


Unicode

是用来在多种双字节字符的格式、编码进行转换的,其中包括一些对这类字符串的操作管理功能中最著名的是UTF-8 编码,它也用一个字节来编码ASCII 字符,这让那些必须同时处理ASCII码和Unicode 码文本的程序员的工作变得非常轻松,因为ASCII 字符的UTF-8 编码跟ASCII 编码完全相同。
Unicode 支持多种编码格式,这为程序员带来了额外的负担,每当你向一个文件写入字符串的时候,你必须定义一个编码(encoding 参数)用于把对应的Unicode 内容转换成你定义的格式,Python 通过Unicode 字符串的encode()函数解决了这个问题,该函数接受字符串中的字符为参数,输出你指定的编码格式的内容。

#!/usr/bin/python

CODEC='utf-8'

FILE='unicode.txt'

hello_out=u"hello world\n"

bytes_out=hello_out.encode(CODEC)

f=open(FILE,"w")

f.write(bytes_out)

f.close()

f=open(FILE,"r")

bytes_in=f.read()

f.close()

hello_in=bytes_in.decode(CODEC)

print hello_in,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: