Python序列——字符串

作者:襄阳西继迅达电梯有限公司  发布时间:2017-09-03 12:06:22
Python序列——字符串

字符串支持序列操作。

1.1 string模块预定义字符串 >>> import string >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.ascii_lowercase 'abcdefghijklmnopqrstuvwxyz' >>> string.ascii_uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.digits '0123456789' >>> 1.2 普通字符串与Unicode字符串 >>> u'Hello' + ' furzoom' u'Hello furzoom' >>> 1.3 只适用于字符串的操作 >>> '%x' % 108 '6c' >>> '%X' % 108 '6C' >>> '%#X' % 108 '0X6C' >>> '%#x' % 108 '0x6c' >>> '%f' % 1234.567890 '1234.567890' >>> '%.2f' % 1234.567890 '1234.57' >>> '%E' % 1234.567890 '1.234568E+03' >>> '%e' % 1234.567890 '1.234568e+03' >>> '%g' % 1234.567890 '1234.57' >>> '%G' % 1234.567890 '1234.57' >>> '%e' % 111111111111111111111 '1.111111e+20' >>> 'Welcome to %(website)s, %(name)s' % {'name': 'mn', 'website': 'furzoom.com'} 'Welcome to furzoom.com, mn' >>> from string import Template >>> s = Template('There are ${howmany} ${lang} Quotation Symbols') >>> print s.substitute(lang='Python', howmany=3) There are 3 Python Quotation Symbols >>> >>> print s.substitute(lang='Python') Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/string.py", line 172, in substitute return self.pattern.sub(convert, self.template) File "/usr/lib/python2.7/string.py", line 162, in convert val = mapping[named] KeyError: 'howmany' >>> >>> print s.safe_substitute(lang='Python') There are ${howmany} Python Quotation Symbols >>> 1.4 原始字符串 >>> '\n' '\n' >>> print '\n' >>> r'\n' '\\n' >>> print r'\n' \n >>> 1.5 Unicode字符串操作符

他用Unicode原始字符串时,u要出现在r前面。

>>> ur'hello\nfurzoom' u'hello\\nfurzoom' >>> ru'hello\nmn' File "", line 1 ru'hello\nmn' ^ SyntaxError: invalid syntax >>> 2. 内建函数 2.1 标准类型函数与序列操作函数 cmp() len() max() min() enumerate() zip() >>> s1 = 'furzoom' >>> s2 = 'abcdefg' >>> cmp(s1, s2) 1 >>> cmp(s2, s1) -1 >>> cmp(s1, 'furzoom') 0 >>> len(s1) 7 >>> max(s1) 'z' >>> min(s1) 'f' >>> us1 = u'furzoom' >>> len(us1) 7 >>> us1 u'furzoom' >>> print us1 furzoom >>> min(us1) u'f' >>> max(us1) u'z' >>> for i, t in enumerate(s1): ... print i, t ... 0 f 1 u 2 r 3 z 4 o 5 o 6 m >>> zip(s2, s1) [('a', 'f'), ('b', 'u'), ('c', 'r'), ('d', 'z'), ('e', 'o'), ('f', 'o'), ('g', 'm')] >>> 2.2 字符串类型函数 raw_input() str() unicode() chr() unichr() ord()

unichr()如果配置为USC2的Unicode,参数范围是range(65535),如果配置为USC4的Unicode,那么参数范围是range(0x1100000)。

>>> name = raw_input("Enter your name: ") Enter your name: furzoom MN >>> name 'furzoom MN' >>> len(name) 10 >>> unicode(name) u'furzoom MN' >>> str(unicode(name)) 'furzoom MN' >>> >>> isinstance(u'\0xAB', str) False >>> isinstance('mn', unicode) False >>> isinstance(u'', unicode) True >>> isinstance('mn', str) True >>> chr(65) 'A' >>> ord('a') 97 >>> unichr(12345) u'\u3039' >>> chr(12345) Traceback (most recent call last): File "", line 1, in ValueError: chr() arg not in range(256) >>> ord(u'\uffff') 65535 >>> 3. 字符串内建函数 string.capitalize() string.center(width[, fillchar]) string.count(sub[, start[, end]]]) string.decode([encode[, errors]]) string.encode([encode[, errors]]) string.endswith(suffix[, start[, end]]]) string.expandtabs([tabsize]) string.find(sub[, start[, end]]) string.format(*args, **kwargs) string.index(sub[, start[, end]]) string.isalnum() string.isalpha() string.isdigit() string.islower() string.isspace() string.istitle() string.isupper() string.istitle() string.join(iterable) string.ljust(width[, fillchar]) string.lower() string.lstrip([chars]) string.partition(sep) string.replace(old, new[, count]) string.rfind(sub[, start[, end]]) string.rindex(sub[, start[, end]]) string.rjust(width[, fillchar]) string.rpartition(sep) string.rsplit([sep[, maxsplit]]) string.rstrip([chars]) string.split([sep[, maxsplit]]) string.splitlines([keepends]) string.startswith(prefix[, start[, end]]) string.strip([chars]) string.swapcase() string.title() string.translate(talbe[, deletechars]) string.upper() string.zfill(width)

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站建设 http://www.feimao666.com

  • 上一篇:网站备案需要什么资料以及网站备案注意事项
  • 下一篇:最后一页
  •