原文链接:http://blog.chinaunix.net/uid-25992400-id-3283846.html

任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作。

    python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求:
  • python的字符串属性函数
  • python的string模块
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1. 字符串属性函数
     系统版本:CentOS release 6.2 (Final)2.6.32-220.el6.x86_64
     python版本:Python 2.6.6

字符串属性方法

字符串格式输出对齐

[python] view plaincopyprint?
  1. 1.>>> str='stRINg lEArn'
  2. 2.>>>
  3. 3.>>> str.center(20)      #生成20个字符长度,str排中间
  4. 4.'    stRINg lEArn    '
  5. 5.>>>
  6. 6.>>> str.ljust(20)       #str左对齐
  7. 7.'stRINg lEArn        '
  8. 8.>>>
  9. 9.>>> str.rjust(20)       #str右对齐
  10. 10.'        stRINg lEArn'
  11. 11.>>>
  12. 12.>>> str.zfill(20)       #str右对齐,左边填充0
  13. 13.'00000000stRINg lEArn'

大小写转换

[python] view plaincopyprint?
  1. 1.>>> str='stRINg lEArn'
  2. 2.>>>
  3. 3.>>> str.upper() #转大写
  4. 4.'STRING LEARN'
  5. 5.>>>
  6. 6.>>> str.lower() #转小写
  7. 7.'string learn'
  8. 8.>>>
  9. 9.>>> str.capitalize() #字符串首为大写,其余小写
  10. 10.'String learn'
  11. 11.>>>
  12. 12.>>> str.swapcase() #大小写对换
  13. 13.'STrinG LeaRN'
  14. 14.>>>
  15. 15.>>> str.title() #以分隔符为标记,首字符为大写,其余为小写
  16. 16.'String Learn'

字符串条件判断

[python] view plaincopyprint?
  1. 1.>>> str='0123'
  2. 2.>>> str.isalnum()  #是否全是字母和数字,并至少有一个字符
  3. 3.True
  4. 4.>>> str.isdigit()   #是否全是数字,并至少有一个字符
  5. 5.True
  6. 6.
  7. 7.>>> str='abcd'
  8. 8.>>> str.isalnum()
  9. 9.True
  10. 10.>>> str.isalpha()   #是否全是字母,并至少有一个字符
  11. 11.True
  12. 12.>>> str.islower()   #是否全是小写,当全是小写和数字一起时候,也判断为True
  13. 13.True
  14. 14.
  15. 15.>>> str='abcd0123'
  16. 16.>>> str.islower()   #同上
  17. 17.True
  18. 18.>>> str.isalnum()
  19. 19.True
  20. 20.
  21. 21.>>> str=' '
  22. 22.>>> str.isspace()    #是否全是空白字符,并至少有一个字符
  23. 23.True
  24. 24.>>> str='ABC'
  25. 25.>>> str.isupper()    #是否全是大写,当全是大写和数字一起时候,也判断为True
  26. 26.True
  27. 27.>>> str='Abb Acc'
  28. 28.>>> str.istitle()    #所有单词字首都是大写,标题
  29. 29.True
  30. 30.
  31. 31.>>> str='string learn'
  32. 32.>>> str.startswith('str') #判断字符串以'str'开头
  33. 33.True
  34. 34.>>> str.endswith('arn')   #判读字符串以'arn'结尾
  35. 35.True

字符串搜索定位与替换

[python] view plaincopyprint?
  1. 1.>>> str='string lEARn'
  2. 2.>>>
  3. 3.>>> str.find('a')      #查找字符串,没有则返回-1,有则返回查到到第一个匹配的索引
  4. 4.-1
  5. 5.>>> str.find('n')
  6. 6.4
  7. 7.>>> str.rfind('n')     #同上,只是返回的索引是最后一次匹配的
  8. 8.11
  9. 9.>>>
  10. 10.>>> str.index('a')     #如果没有匹配则报错
  11. 11.Traceback (most recent call last):
  12. 12.  File "<stdin>", line 1, in <module>
  13. 13.ValueError: substring not found
  14. 14.>>> str.index('n')     #同find类似,返回第一次匹配的索引值
  15. 15.4
  16. 16.>>> str.rindex('n')    #返回最后一次匹配的索引值
  17. 17.11
  18. 18.>>>
  19. 19.>>> str.count('a')     #字符串中匹配的次数
  20. 20.0
  21. 21.>>> str.count('n')     #同上
  22. 22.2
  23. 23.>>>
  24. 24.>>> str.replace('EAR','ear')  #匹配替换
  25. 25.'string learn'
  26. 26.>>> str.replace('n','N')
  27. 27.'striNg lEARN'
  28. 28.>>> str.replace('n','N',1)
  29. 29.'striNg lEARn'
  30. 30.>>>
  31. 31.>>>
  32. 32.>>> str.strip('n')   #删除字符串首尾匹配的字符,通常用于默认删除回车符
  33. 33.'string lEAR'
  34. 34.>>> str.lstrip('n')  #左匹配
  35. 35.'string lEARn'
  36. 36.>>> str.rstrip('n')  #右匹配
  37. 37.'string lEAR'
  38. 38.>>>
  39. 39.>>> str='   tab'
  40. 40.>>> str.expandtabs()  #把制表符转为空格
  41. 41.'      tab'
  42. 42.>>> str.expandtabs(2) #指定空格数
  43. 43.' tab'

字符串编码与解码

[python] view plaincopyprint?
  1. 1.>>> str='字符串学习'
  2. 2.>>> str
  3. 3.'xe5xadx97xe7xacxa6xe4xb8xb2xe5xadxa6xe4xb9xa0'
  4. 4.>>>
  5. 5.>>> str.decode('utf-8')               #解码过程,将utf-8解码为unicode
  6. 6.u'u5b57u7b26u4e32u5b66u4e60'
  7. 7.
  8. 8.>>> str.decode('utf-8').encode('gbk')  #编码过程,将unicode编码为gbk
  9. 9.'xd7xd6xb7xfbxb4xaexd1xa7xcfxb0'
  10. 10.>>> str.decode('utf-8').encode('utf-8')  #将unicode编码为utf-8
  11. 11.'xe5xadx97xe7xacxa6xe4xb8xb2xe5xadxa6xe4xb9xa0'

字符串分割变换

[python] view plaincopyprint?
  1. 1.>>> str='Learn string'
  2. 2.>>> '-'.join(str)
  3. 3.'L-e-a-r-n- -s-t-r-i-n-g'
  4. 4.>>> l1=['Learn','string']
  5. 5.>>> '-'.join(l1)
  6. 6.'Learn-string'
  7. 7.>>>
  8. 8.>>> str.split('n')
  9. 9.['Lear', ' stri', 'g']
  10. 10.>>> str.split('n',1)
  11. 11.['Lear', ' string']
  12. 12.>>> str.rsplit('n',1)
  13. 13.['Learn stri', 'g']
  14. 14.>>>
  15. 15.>>> str.splitlines()
  16. 16.['Learn string']
  17. 17.>>>
  18. 18.>>> str.partition('n')
  19. 19.('Lear', 'n', ' string')
  20. 20.>>> str.rpartition('n')
  21. 21.('Learn stri', 'n', 'g')

string模块源代码

[python] view plaincopyprint?
  1. 1."""A collection of string operations (most are no longer used).
  2. 2.
  3. 3.Warning: most of the code you see here isn't normally used nowadays.
  4. 4.Beginning with Python 1.6, many of these functions are implemented as
  5. 5.methods on the standard string object. They used to be implemented by
  6. 6.a built-in module called strop, but strop is now obsolete itself.
  7. 7.
  8. 8.Public module variables:
  9. 9.
  10. 10.whitespace -- a string containing all characters considered whitespace
  11. 11.lowercase -- a string containing all characters considered lowercase letters
  12. 12.uppercase -- a string containing all characters considered uppercase letters
  13. 13.letters -- a string containing all characters considered letters
  14. 14.digits -- a string containing all characters considered decimal digits
  15. 15.hexdigits -- a string containing all characters considered hexadecimal digits
  16. 16.octdigits -- a string containing all characters considered octal digits
  17. 17.punctuation -- a string containing all characters considered punctuation
  18. 18.printable -- a string containing all characters considered printable
  19. 19.
  20. 20."""
  21. 21.
  22. 22.# Some strings for ctype-style character classification
  23. 23.whitespace = ' tnrvf'
  24. 24.lowercase = 'abcdefghijklmnopqrstuvwxyz'
  25. 25.uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  26. 26.letters = lowercase + uppercase
  27. 27.ascii_lowercase = lowercase
  28. 28.ascii_uppercase = uppercase
  29. 29.ascii_letters = ascii_lowercase + ascii_uppercase
  30. 30.digits = '0123456789'
  31. 31.hexdigits = digits + 'abcdef' + 'ABCDEF'
  32. 32.octdigits = '01234567'
  33. 33.punctuation = """!"#$%&'()*+,-./:;<=>?@[]^_`{|}~"""
  34. 34.printable = digits + letters + punctuation + whitespace
  35. 35.
  36. 36.# Case conversion helpers
  37. 37.# Use str to convert Unicode literal in case of -U
  38. 38.l = map(chr, xrange(256))
  39. 39._idmap = str('').join(l)
  40. 40.del l
  41. 41.
  42. 42.# Functions which aren't available as string methods.
  43. 43.
  44. 44.# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
  45. 45.def capwords(s, sep=None):
  46. 46.    """capwords(s [,sep]) -> string
  47. 47.
  48. 48.    Split the argument into words using split, capitalize each
  49. 49.    word using capitalize, and join the capitalized words using
  50. 50.    join. If the optional second argument sep is absent or None,
  51. 51.    runs of whitespace characters are replaced by a single space
  52. 52.    and leading and trailing whitespace are removed, otherwise
  53. 53.    sep is used to split and join the words.
  54. 54.
  55. 55.    """
  56. 56.    return (sep or ' ').join(x.capitalize() for x in s.split(sep))
  57. 57.
  58. 58.
  59. 59.# Construct a translation string
  60. 60._idmapL = None
  61. 61.def maketrans(fromstr, tostr):
  62. 62.    """maketrans(frm, to) -> string
  63. 63.
  64. 64.    Return a translation table (a string of 256 bytes long)
  65. 65.    suitable for use in string.translate. The strings frm and to
  66. 66.    must be of the same length.
  67. 67.
  68. 68.    """
  69. 69.    if len(fromstr) != len(tostr):
  70. 70.        raise ValueError, "maketrans arguments must have same length"
  71. 71.    global _idmapL
  72. 72.    if not _idmapL:
  73. 73.        _idmapL = list(_idmap)
  74. 74.    L = _idmapL[:]
  75. 75.    fromstr = map(ord, fromstr)
  76. 76.    for i in range(len(fromstr)):
  77. 77.        L[fromstr[i]] = tostr[i]
  78. 78.    return ''.join(L)
  79. 79.
  80. 80.
  81. 81.
  82. 82.####################################################################
  83. 83.import re as _re
  84. 84.
  85. 85.class _multimap:
  86. 86.    """Helper class for combining multiple mappings.
  87. 87.
  88. 88.    Used by .{safe_,}substitute() to combine the mapping and keyword
  89. 89.    arguments.
  90. 90.    """
  91. 91.    def __init__(self, primary, secondary):
  92. 92.        self._primary = primary
  93. 93.        self._secondary = secondary
  94. 94.
  95. 95.    def __getitem__(self, key):
  96. 96.        try:
  97. 97.            return self._primary[key]
  98. 98.        except KeyError:
  99. 99.            return self._secondary[key]
  100. 100.
  101. 101.
  102. 102.class _TemplateMetaclass(type):
  103. 103.    pattern = r"""
  104. 104.    %(delim)s(?:
  105. 105.      (?P<escaped>%(delim)s) | # Escape sequence of two delimiters
  106. 106.      (?P<named>%(id)s) | # delimiter and a Python identifier
  107. 107.      {(?P<braced>%(id)s)} | # delimiter and a braced identifier
  108. 108.      (?P<invalid>) # Other ill-formed delimiter exprs
  109. 109.    )
  110. 110.    """
  111. 111.
  112. 112.    def __init__(cls, name, bases, dct):
  113. 113.        super(_TemplateMetaclass, cls).__init__(name, bases, dct)
  114. 114.        if 'pattern' in dct:
  115. 115.            pattern = cls.pattern
  116. 116.        else:
  117. 117.            pattern = _TemplateMetaclass.pattern % {
  118. 118.                'delim' : _re.escape(cls.delimiter),
  119. 119.                'id' : cls.idpattern,
  120. 120.                }
  121. 121.        cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)
  122. 122.
  123. 123.
  124. 124.class Template:
  125. 125.    """A string class for supporting $-substitutions."""
  126. 126.    __metaclass__ = _TemplateMetaclass
  127. 127.
  128. 128.    delimiter = '$'
  129. 129.    idpattern = r'[_a-z][_a-z0-9]*'
  130. 130.
  131. 131.    def __init__(self, template):
  132. 132.        self.template = template
  133. 133.
  134. 134.    # Search for $$, $identifier, ${identifier}, and any bare $'s
  135. 135.
  136. 136.    def _invalid(self, mo):
  137. 137.        i = mo.start('invalid')
  138. 138.        lines = self.template[:i].splitlines(True)
  139. 139.        if not lines:
  140. 140.            colno = 1
  141. 141.            lineno = 1
  142. 142.        else:
  143. 143.            colno = i - len(''.join(lines[:-1]))
  144. 144.            lineno = len(lines)
  145. 145.        raise ValueError('Invalid placeholder in string: line %d, col %d' %
  146. 146.                         (lineno, colno))
  147. 147.
  148. 148.    def substitute(self, *args, **kws):
  149. 149.        if len(args) > 1:
  150. 150.            raise TypeError('Too many positional arguments')
  151. 151.        if not args:
  152. 152.            mapping = kws
  153. 153.        elif kws:
  154. 154.            mapping = _multimap(kws, args[0])
  155. 155.        else:
  156. 156.            mapping = args[0]
  157. 157.        # Helper function for .sub()
  158. 158.        def convert(mo):
  159. 159.            # Check the most common path first.
  160. 160.            named = mo.group('named') or mo.group('braced')
  161. 161.            if named is not None:
  162. 162.                val = mapping[named]
  163. 163.                # We use this idiom instead of str() because the latter will
  164. 164.                # fail if val is a Unicode containing non-ASCII characters.
  165. 165.                return '%s' % (val,)
  166. 166.            if mo.group('escaped') is not None:
  167. 167.                return self.delimiter
  168. 168.            if mo.group('invalid') is not None:
  169. 169.                self._invalid(mo)
  170. 170.            raise ValueError('Unrecognized named group in pattern',
  171. 171.                             self.pattern)
  172. 172.        return self.pattern.sub(convert, self.template)
  173. 173.
  174. 174.    def safe_substitute(self, *args, **kws):
  175. 175.        if len(args) > 1:
  176. 176.            raise TypeError('Too many positional arguments')
  177. 177.        if not args:
  178. 178.            mapping = kws
  179. 179.        elif kws:
  180. 180.            mapping = _multimap(kws, args[0])
  181. 181.        else:
  182. 182.            mapping = args[0]
  183. 183.        # Helper function for .sub()
  184. 184.        def convert(mo):
  185. 185.            named = mo.group('named')
  186. 186.            if named is not None:
  187. 187.                try:
  188. 188.                    # We use this idiom instead of str() because the latter
  189. 189.                    # will fail if val is a Unicode containing non-ASCII
  190. 190.                    return '%s' % (mapping[named],)
  191. 191.                except KeyError:
  192. 192.                    return self.delimiter + named
  193. 193.            braced = mo.group('braced')
  194. 194.            if braced is not None:
  195. 195.                try:
  196. 196.                    return '%s' % (mapping[braced],)
  197. 197.                except KeyError:
  198. 198.                    return self.delimiter + '{' + braced + '}'
  199. 199.            if mo.group('escaped') is not None:
  200. 200.                return self.delimiter
  201. 201.            if mo.group('invalid') is not None:
  202. 202.                return self.delimiter
  203. 203.            raise ValueError('Unrecognized named group in pattern',
  204. 204.                             self.pattern)
  205. 205.        return self.pattern.sub(convert, self.template)
  206. 206.
  207. 207.
  208. 208.
  209. 209.####################################################################
  210. 210.# NOTE: Everything below here is deprecated. Use string methods instead.
  211. 211.# This stuff will go away in Python 3.0.
  212. 212.
  213. 213.# Backward compatible names for exceptions
  214. 214.index_error = ValueError
  215. 215.atoi_error = ValueError
  216. 216.atof_error = ValueError
  217. 217.atol_error = ValueError
  218. 218.
  219. 219.# convert UPPER CASE letters to lower case
  220. 220.def lower(s):
  221. 221.    """lower(s) -> string
  222. 222.
  223. 223.    Return a copy of the string s converted to lowercase.
  224. 224.
  225. 225.    """
  226. 226.    return s.lower()
  227. 227.
  228. 228.# Convert lower case letters to UPPER CASE
  229. 229.def upper(s):
  230. 230.    """upper(s) -> string
  231. 231.
  232. 232.    Return a copy of the string s converted to uppercase.
  233. 233.
  234. 234.    """
  235. 235.    return s.upper()
  236. 236.
  237. 237.# Swap lower case letters and UPPER CASE
  238. 238.def swapcase(s):
  239. 239.    """swapcase(s) -> string
  240. 240.
  241. 241.    Return a copy of the string s with upper case characters
  242. 242.    converted to lowercase and vice versa.
  243. 243.
  244. 244.    """
  245. 245.    return s.swapcase()
  246. 246.
  247. 247.# Strip leading and trailing tabs and spaces
  248. 248.def strip(s, chars=None):
  249. 249.    """strip(s [,chars]) -> string
  250. 250.
  251. 251.    Return a copy of the string s with leading and trailing
  252. 252.    whitespace removed.
  253. 253.    If chars is given and not None, remove characters in chars instead.
  254. 254.    If chars is unicode, S will be converted to unicode before stripping.
  255. 255.
  256. 256.    """
  257. 257.    return s.strip(chars)
  258. 258.
  259. 259.# Strip leading tabs and spaces
  260. 260.def lstrip(s, chars=None):
  261. 261.    """lstrip(s [,chars]) -> string
  262. 262.
  263. 263.    Return a copy of the string s with leading whitespace removed.
  264. 264.    If chars is given and not None, remove characters in chars instead.
  265. 265.
  266. 266.    """
  267. 267.    return s.lstrip(chars)
  268. 268.
  269. 269.# Strip trailing tabs and spaces
  270. 270.def rstrip(s, chars=None):
  271. 271.    """rstrip(s [,chars]) -> string
  272. 272.
  273. 273.    Return a copy of the string s with trailing whitespace removed.
  274. 274.    If chars is given and not None, remove characters in chars instead.
  275. 275.
  276. 276.    """
  277. 277.    return s.rstrip(chars)
  278. 278.
  279. 279.
  280. 280.# Split a string into a list of space/tab-separated words
  281. 281.def split(s, sep=None, maxsplit=-1):
  282. 282.    """split(s [,sep [,maxsplit]]) -> list of strings
  283. 283.
  284. 284.    Return a list of the words in the string s, using sep as the
  285. 285.    delimiter string. If maxsplit is given, splits at no more than
  286. 286.    maxsplit places (resulting in at most maxsplit+1 words). If sep
  287. 287.    is not specified or is None, any whitespace string is a separator.
  288. 288.
  289. 289.    (split and splitfields are synonymous)
  290. 290.
  291. 291.    """
  292. 292.    return s.split(sep, maxsplit)
  293. 293.splitfields = split
  294. 294.
  295. 295.# Split a string into a list of space/tab-separated words
  296. 296.def rsplit(s, sep=None, maxsplit=-1):
  297. 297.    """rsplit(s [,sep [,maxsplit]]) -> list of strings
  298. 298.
  299. 299.    Return a list of the words in the string s, using sep as the
  300. 300.    delimiter string, starting at the end of the string and working
  301. 301.    to the front. If maxsplit is given, at most maxsplit splits are
  302. 302.    done. If sep is not specified or is None, any whitespace string
  303. 303.    is a separator.
  304. 304.    """
  305. 305.    return s.rsplit(sep, maxsplit)
  306. 306.
  307. 307.# Join fields with optional separator
  308. 308.def join(words, sep = ' '):
  309. 309.    """join(list [,sep]) -> string
  310. 310.
  311. 311.    Return a string composed of the words in list, with
  312. 312.    intervening occurrences of sep. The default separator is a
  313. 313.    single space.
  314. 314.
  315. 315.    (joinfields and join are synonymous)
  316. 316.
  317. 317.    """
  318. 318.    return sep.join(words)
  319. 319.joinfields = join
  320. 320.
  321. 321.# Find substring, raise exception if not found
  322. 322.def index(s, *args):
  323. 323.    """index(s, sub [,start [,end]]) -> int
  324. 324.
  325. 325.    Like find but raises ValueError when the substring is not found.
  326. 326.
  327. 327.    """
  328. 328.    return s.index(*args)
  329. 329.
  330. 330.# Find last substring, raise exception if not found
  331. 331.def rindex(s, *args):
  332. 332.    """rindex(s, sub [,start [,end]]) -> int
  333. 333.
  334. 334.    Like rfind but raises ValueError when the substring is not found.
  335. 335.
  336. 336.    """
  337. 337.    return s.rindex(*args)
  338. 338.
  339. 339.# Count non-overlapping occurrences of substring
  340. 340.def count(s, *args):
  341. 341.    """count(s, sub[, start[,end]]) -> int
  342. 342.
  343. 343.    Return the number of occurrences of substring sub in string
  344. 344.    s[start:end]. Optional arguments start and end are
  345. 345.    interpreted as in slice notation.
  346. 346.
  347. 347.    """
  348. 348.    return s.count(*args)
  349. 349.
  350. 350.# Find substring, return -1 if not found
  351. 351.def find(s, *args):
  352. 352.    """find(s, sub [,start [,end]]) -> in
  353. 353.
  354. 354.    Return the lowest index in s where substring sub is found,
  355. 355.    such that sub is contained within s[start,end]. Optional
  356. 356.    arguments start and end are interpreted as in slice notation.
  357. 357.
  358. 358.    Return -1 on failure.
  359. 359.
  360. 360.    """
  361. 361.    return s.find(*args)
  362. 362.
  363. 363.# Find last substring, return -1 if not found
  364. 364.def rfind(s, *args):
  365. 365.    """rfind(s, sub [,start [,end]]) -> int
  366. 366.
  367. 367.    Return the highest index in s where substring sub is found,
  368. 368.    such that sub is contained within s[start,end]. Optional
  369. 369.    arguments start and end are interpreted as in slice notation.
  370. 370.
  371. 371.    Return -1 on failure.
  372. 372.
  373. 373.    """
  374. 374.    return s.rfind(*args)
  375. 375.
  376. 376.# for a bit of speed
  377. 377._float = float
  378. 378._int = int
  379. 379._long = long
  380. 380.
  381. 381.# Convert string to float
  382. 382.def atof(s):
  383. 383.    """atof(s) -> float
  384. 384.
  385. 385.    Return the floating point number represented by the string s.
  386. 386.
  387. 387.    """
  388. 388.    return _float(s)
  389. 389.
  390. 390.
  391. 391.# Convert string to integer
  392. 392.def atoi(s , base=10):
  393. 393.    """atoi(s [,base]) -> int
  394. 394.
  395. 395.    Return the integer represented by the string s in the given
  396. 396.    base, which defaults to 10. The string s must consist of one
  397. 397.    or more digits, possibly preceded by a sign. If base is 0, it
  398. 398.    is chosen from the leading characters of s, 0 for octal, 0x or
  399. 399.    0X for hexadecimal. If base is 16, a preceding 0x or 0X is
  400. 400.    accepted.
  401. 401.
  402. 402.    """
  403. 403.    return _int(s, base)
  404. 404.
  405. 405.
  406. 406.# Convert string to long integer
  407. 407.def atol(s, base=10):
  408. 408.    """atol(s [,base]) -> long
  409. 409.
  410. 410.    Return the long integer represented by the string s in the
  411. 411.    given base, which defaults to 10. The string s must consist
  412. 412.    of one or more digits, possibly preceded by a sign. If base
  413. 413.    is 0, it is chosen from the leading characters of s, 0 for
  414. 414.    octal, 0x or 0X for hexadecimal. If base is 16, a preceding
  415. 415.    0x or 0X is accepted. A trailing L or l is not accepted,
  416. 416.    unless base is 0.
  417. 417.
  418. 418.    """
  419. 419.    return _long(s, base)
  420. 420.
  421. 421.
  422. 422.# Left-justify a string
  423. 423.def ljust(s, width, *args):
  424. 424.    """ljust(s, width[, fillchar]) -> string
  425. 425.
  426. 426.    Return a left-justified version of s, in a field of the
  427. 427.    specified width, padded with spaces as needed. The string is
  428. 428.    never truncated. If specified the fillchar is used instead of spaces.
  429. 429.
  430. 430.    """
  431. 431.    return s.ljust(width, *args)
  432. 432.
  433. 433.# Right-justify a string
  434. 434.def rjust(s, width, *args):
  435. 435.    """rjust(s, width[, fillchar]) -> string
  436. 436.
  437. 437.    Return a right-justified version of s, in a field of the
  438. 438.    specified width, padded with spaces as needed. The string is
  439. 439.    never truncated. If specified the fillchar is used instead of spaces.
  440. 440.
  441. 441.    """
  442. 442.    return s.rjust(width, *args)
  443. 443.
  444. 444.# Center a string
  445. 445.def center(s, width, *args):
  446. 446.    """center(s, width[, fillchar]) -> string
  447. 447.
  448. 448.    Return a center version of s, in a field of the specified
  449. 449.    width. padded with spaces as needed. The string is never
  450. 450.    truncated. If specified the fillchar is used instead of spaces.
  451. 451.
  452. 452.    """
  453. 453.    return s.center(width, *args)
  454. 454.
  455. 455.# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
  456. 456.# Decadent feature: the argument may be a string or a number
  457. 457.# (Use of this is deprecated; it should be a string as with ljust c.s.)
  458. 458.def zfill(x, width):
  459. 459.    """zfill(x, width) -> string
  460. 460.
  461. 461.    Pad a numeric string x with zeros on the left, to fill a field
  462. 462.    of the specified width. The string x is never truncated.
  463. 463.
  464. 464.    """
  465. 465.    if not isinstance(x, basestring):
  466. 466.        x = repr(x)
  467. 467.    return x.zfill(width)
  468. 468.
  469. 469.# Expand tabs in a string.
  470. 470.# Doesn't take non-printing chars into account, but does understand n.
  471. 471.def expandtabs(s, tabsize=8):
  472. 472.    """expandtabs(s [,tabsize]) -> string
  473. 473.
  474. 474.    Return a copy of the string s with all tab characters replaced
  475. 475.    by the appropriate number of spaces, depending on the current
  476. 476.    column, and the tabsize (default 8).
  477. 477.
  478. 478.    """
  479. 479.    return s.expandtabs(tabsize)
  480. 480.
  481. 481.# Character translation through look-up table.
  482. 482.def translate(s, table, deletions=""):
  483. 483.    """translate(s,table [,deletions]) -> string
  484. 484.
  485. 485.    Return a copy of the string s, where all characters occurring
  486. 486.    in the optional argument deletions are removed, and the
  487. 487.    remaining characters have been mapped through the given
  488. 488.    translation table, which must be a string of length 256. The
  489. 489.    deletions argument is not allowed for Unicode strings.
  490. 490.
  491. 491.    """
  492. 492.    if deletions or table is None:
  493. 493.        return s.translate(table, deletions)
  494. 494.    else:
  495. 495.        # Add s[:0] so that if s is Unicode and table is an 8-bit string,
  496. 496.        # table is converted to Unicode. This means that table *cannot*
  497. 497.        # be a dictionary -- for that feature, use u.translate() directly.
  498. 498.        return s.translate(table + s[:0])
  499. 499.
  500. 500.# Capitalize a string, e.g. "aBc dEf" -> "Abc def".
  501. 501.def capitalize(s):
  502. 502.    """capitalize(s) -> string
  503. 503.
  504. 504.    Return a copy of the string s with only its first character
  505. 505.    capitalized.
  506. 506.
  507. 507.    """
  508. 508.    return s.capitalize()
  509. 509.
  510. 510.# Substring replacement (global)
  511. 511.def replace(s, old, new, maxsplit=-1):
  512. 512.    """replace (str, old, new[, maxsplit]) -> string
  513. 513.
  514. 514.    Return a copy of string str with all occurrences of substring
  515. 515.    old replaced by new. If the optional argument maxsplit is
  516. 516.    given, only the first maxsplit occurrences are replaced.
  517. 517.
  518. 518.    """
  519. 519.    return s.replace(old, new, maxsplit)
  520. 520.
  521. 521.
  522. 522.# Try importing optional built-in module "strop" -- if it exists,
  523. 523.# it redefines some string operations that are 100-1000 times faster.
  524. 524.# It also defines values for whitespace, lowercase and uppercase
  525. 525.# that match <ctype.h>'s definitions.
  526. 526.
  527. 527.try:
  528. 528.    from strop import maketrans, lowercase, uppercase, whitespace
  529. 529.    letters = lowercase + uppercase
  530. 530.except ImportError:
  531. 531.    pass # Use the original versions
  532. 532.
  533. 533.########################################################################
  534. 534.# the Formatter class
  535. 535.# see PEP 3101 for details and purpose of this class
  536. 536.
  537. 537.# The hard parts are reused from the C implementation. They're exposed as "_"
  538. 538.# prefixed methods of str and unicode.
  539. 539.
  540. 540.# The overall parser is implemented in str._formatter_parser.
  541. 541.# The field name parser is implemented in str._formatter_field_name_split
  542. 542.
  543. 543.class Formatter(object):
  544. 544.    def format(self, format_string, *args, **kwargs):
  545. 545.        return self.vformat(format_string, args, kwargs)
  546. 546.
  547. 547.    def vformat(self, format_string, args, kwargs):
  548. 548.        used_args = set()
  549. 549.        result = self._vformat(format_string, args, kwargs, used_args, 2)
  550. 550.        self.check_unused_args(used_args, args, kwargs)
  551. 551.        return result
  552. 552.
  553. 553.    def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
  554. 554.        if recursion_depth < 0:
  555. 555.            raise ValueError('Max string recursion exceeded')
  556. 556.        result = []
  557. 557.        for literal_text, field_name, format_spec, conversion in
  558. 558.                self.parse(format_string):
  559. 559.
  560. 560.            # output the literal text
  561. 561.            if literal_text:
  562. 562.                result.append(literal_text)
  563. 563.
  564. 564.            # if there's a field, output it
  565. 565.            if field_name is not None:
  566. 566.                # this is some markup, find the object and do
  567. 567.                # the formatting
  568. 568.
  569. 569.                # given the field_name, find the object it references
  570. 570.                # and the argument it came from
  571. 571.                obj, arg_used = self.get_field(field_name, args, kwargs)
  572. 572.                used_args.add(arg_used)
  573. 573.
  574. 574.                # do any conversion on the resulting object
  575. 575.                obj = self.convert_field(obj, conversion)
  576. 576.
  577. 577.                # expand the format spec, if needed
  578. 578.                format_spec = self._vformat(format_spec, args, kwargs,
  579. 579.                                            used_args, recursion_depth-1)
  580. 580.
  581. 581.                # format the object and append to the result
  582. 582.                result.append(self.format_field(obj, format_spec))
  583. 583.
  584. 584.        return ''.join(result)
  585. 585.
  586. 586.
  587. 587.    def get_value(self, key, args, kwargs):
  588. 588.        if isinstance(key, (int, long)):
  589. 589.            return args[key]
  590. 590.        else:
  591. 591.            return kwargs[key]
  592. 592.
  593. 593.
  594. 594.    def check_unused_args(self, used_args, args, kwargs):
  595. 595.        pass
  596. 596.
  597. 597.
  598. 598.    def format_field(self, value, format_spec):
  599. 599.        return format(value, format_spec)
  600. 600.
  601. 601.
  602. 602.    def convert_field(self, value, conversion):
  603. 603.        # do any conversion on the resulting object
  604. 604.        if conversion == 'r':
  605. 605.            return repr(value)
  606. 606.        elif conversion == 's':
  607. 607.            return str(value)
  608. 608.        elif conversion is None:
  609. 609.            return value
  610. 610.        raise ValueError("Unknown converion specifier {0!s}".format(conversion))
  611. 611.
  612. 612.
  613. 613.    # returns an iterable that contains tuples of the form:
  614. 614.    # (literal_text, field_name, format_spec, conversion)
  615. 615.    # literal_text can be zero length
  616. 616.    # field_name can be None, in which case there's no
  617. 617.    # object to format and output
  618. 618.    # if field_name is not None, it is looked up, formatted
  619. 619.    # with format_spec and conversion and then used
  620. 620.    def parse(self, format_string):
  621. 621.        return format_string._formatter_parser()
  622. 622.
  623. 623.
  624. 624.    # given a field_name, find the object it references.
  625. 625.    # field_name: the field being looked up, e.g. "0.name"
  626. 626.    # or "lookup[3]"
  627. 627.    # used_args: a set of which args have been used
  628. 628.    # args, kwargs: as passed in to vformat
  629. 629.    def get_field(self, field_name, args, kwargs):
  630. 630.        first, rest = field_name._formatter_field_name_split()
  631. 631.
  632. 632.        obj = self.get_value(first, args, kwargs)
  633. 633.
  634. 634.        # loop through the rest of the field_name, doing
  635. 635.        # getattr or getitem as needed
  636. 636.        for is_attr, i in rest:
  637. 637.            if is_attr:
  638. 638.                obj = getattr(obj, i)
  639. 639.            else:
  640. 640.                obj = obj[i]
  641. 641.
  642. 642.        return obj, first

python中string模块各属性以及函数的用法相关推荐

  1. 牛人总结python中string模块各属性以及函数的用法,果断转了,好东西

    原文链接:http://blog.chinaunix.net/uid-25992400-id-3283846.html 任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产 ...

  2. [转载] python中string函数的用法_python中string模块各属性以及函数的用法

    参考链接: Python中的string.octdigits 任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作. python的字符 ...

  3. python isalpha函数用法_python中string模块各属性以及函数的用法

    任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作. python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求 ...

  4. python中strip()、lstrip()、rstrip()函数的用法详解

    一.strip()函数的简单应用 1.strip()函数 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. 注意:该方法只能删除开头或是结尾的字符,不能 ...

  5. python中functools_functools模块2个常用函数

    functools是python2.5被引人的,一些工具函数放在此包里,python3中增加了更多工具函数,业务开发时大多情况下用不到,此处介绍使用频率较高的2个函数. partial函数(偏函数) ...

  6. python中islower()、isupper()、istitle()函数的用法详解

    一.python判断字符串大小写 string.islower() 判断字符串的区分大小写的字符是否全为小写. string.isupper() 判断字符串的的字符是否全为大写. string.ist ...

  7. python中numpy模块下的np.clip()的用法

    Numpy 中clip函数的使用 numpy.clip(a, a_min, a_max, out=None) Clip (limit) the values in an array. Given an ...

  8. python 中 numpy 模块的 size,shape, len的用法

    numpy 中有很多类方法可以对数组处理,下面将介绍三种常见的处理数组的方法. 1.size的用法 import numpy as np X=np.array([[1,2,3,4],[5,6,7,8] ...

  9. python跨函数调用变量_对python中不同模块(函数、类、变量)的调用详解

    首先,先介绍两种引入模块的方法. 法一:将整个文件引入 import 文件名 文件名.函数名( ) / 文件名.类名 通过这个方法可以运行另外一个文件里的函数 法二:只引入某个文件中一个类/函数/变量 ...

最新文章

  1. Rosalind: 兔子与递归
  2. python np fft_Python的武器库05:numpy模块(下)
  3. C#网络编程(基本概念和操作) - Part.1
  4. 05.analysis-normalizer应用
  5. 【CC精品教程】ContextCapture 4.4.12(CC,Smart 3D)简体中文版安装教程(附安装包下载)
  6. 实话!程序员大都不喜欢拉帮结派
  7. 50T内存?百万机时?头一次见这么耗费内存和机时的分析?
  8. 一图解惑SQL JOINS
  9. 文科生必备计算机知识点,文科生计算机知识点调查报告.docx
  10. Hibernate Session中的save(),update(),delete(),saveOrUpdate() 细粒度分析
  11. python语法注释原则
  12. Android学习笔记之百度地图基础知识
  13. SQL查询时间段方法
  14. 微信小程序生成分享图然后保存图片分享朋友圈
  15. One-Error多标签分类_多分类及多标签分类算法
  16. 论从容自信---张含韵和涛声依旧有感
  17. 马云:30年后每对年轻人要养8个老人 管理5套房子
  18. 笔记-编译原理-实验四-语义分析与中间代码生成
  19. 【更新公告】Airtest更新至1.2.4
  20. 摸鱼加速小能手,实用笔刷快拿走

热门文章

  1. 9 年小厂老前端的年终总结(90 后,12 年毕业,工作 9 年,发过传单,做过运营)
  2. 《狂飙》壁纸太帅,Python自动切换太酷(8)
  3. zabbix 监控下载安装
  4. Dell服务器装系统黑屏,简单几步解决dell win7黑屏_dell win7黑屏如何修复
  5. 如何把图片转化成excel表格?
  6. python多图片合并pdf_Python结合ImageMagick实现多张图片合并为一个pdf文件的方法
  7. 莫名骨痛,警惕骨转移
  8. Linux下的压缩解压缩工具(转载)
  9. SpreadJS 表格控件相关基础知识
  10. 云计算技术文章的创作心得