common string oprations
import string
1. string constants(常量)

1) string.ascii_letters
      The concatenation of the ascii_lowercase and ascii_uppercase constants described below. This value is not locale-dependent.
print string.ascii_letters
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

2) string.ascii_lowercase
     
 The lowercase letters 'abcdefghijklmnopqrstuvwxyz'. This value is not locale-dependent and will not change.

3) string.ascii_uppercase
      The uppercase letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. This value is not locale-dependent and will not change.
4) string.digits 十进制
      The string '0123456789'.

5) string.hexdigits 十六进制
    The string '0123456789abcdefABCDEF'.
6) string.letters
    The concatenation of the strings lowercase and uppercase described below. The specific value is locale-dependent, and will be updated when locale.setlocale() is called.

print string.letters
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

7) string.lowercase
    A string containing all the characters that are considered lowercase letters. On most systems this is the string 'abcdefghijklmnopqrstuvwxyz'. Do not change its definition — the effect on the routines upper() and swapcase() is undefined. The specific value is locale-dependent, and will be updated when locale.setlocale() is called.

8) string.octdigits 八进制
    The string '01234567'.

9) string.punctuation 标点符号
    String of ASCII characters which are considered punctuation characters in the C locale.

10) string.printable 所有可打印的字符集,包含数字,字母,标点空白符
    String of characters which are considered printable. This is a combination of digits, letters, punctuation, and whitespace.
print string.printable
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

11) string.uppercase
    A string containing all the characters that are considered uppercase letters. On most systems this is the string 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. Do not change its definition — the effect on the routines lower() and swapcase() is undefined. The specific value is locale-dependent, and will be updated when locale.setlocale() is called.

12) string.whitespace 所有的空白符包含
\t  制表符
\n 换行符 (linefeed)

\x0b

\x0C
\r 不要改变这个定义──因为所影响它的方法strip()和split()为被定义

A string containing all characters that are considered whitespace. On most systems this includes the characters space, tab, linefeed, return, formfeed, and vertical tab. Do not change its definition — the effect on the routines strip() and split() is undefined.
print string.whitespace

2. string Method(方法)
Below are listed the string methods which both 8-bit strings and Unicode objects support. Note that none of these methods take keyword arguments.

In addition, Python’s strings support the sequence type methods described in the Sequence Types — str, unicode, list, tuple, buffer, xrange section. To output formatted strings use template strings or the % operator described in the String Formatting Operations section. Also, see the re module for string functions based on regular expression_rs.

str.capitalize() 将字符串的第一个字母变成大写,其他字母变小写
    Return a copy of the string with only its first character capitalized.
    For 8-bit strings, this method is locale-dependent.
str.center(width[, fillchar])
    Return centered in a string of length width. Padding is done using the specified fillchar (default is a space).
    Changed in version 2.4: Support for the fillchar argument.
str.count(sub[, start[, end]]) 返回sub子串的数量
    Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.
str.decode([encoding[, errors]]) 解码
    Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding. errors may be given to set a different error handling scheme. The default is 'strict', meaning that encoding errors raise UnicodeError. Other possible values are 'ignore', 'replace' and any other name registered via codecs.register_error(), see section Codec Base Classes.
str.encode([encoding[, errors]]) 编码
    Return an encoded version of the string. Default encoding is the current default string encoding. errors may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' and any other name registered via codecs.register_error(), see section Codec Base Classes. For a list of possible encodings, see section Standard Encodings.
str.endswith(suffix[, start[, end]]) 检查字符串是否suffix来结尾的,是返回true,否返回false.
    Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.
str.expandtabs([tabsize])
    Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. The column number is reset to zero after each newline occurring in the string. If tabsize is not given, a tab size of 8 characters is assumed. This doesn’t understand other non-printing characters or escape sequences.
str.find(sub[, start[, end]])在字符串中查找sub,找到后返回位置,没找到返回-1.
    Return the lowest index in the string where substring sub is found, such that sub is contained in the range [start, end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.
str.format(format_string, *args, **kwargs)
    Perform a string formatting operation. The format_string argument can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of format_string where each replacement field is replaced with the string value of the corresponding argument.
    >>> "The sum of 1 + 2 is {0}".format(1+2)
    'The sum of 1 + 2 is 3'
    See Format String Syntax for a description of the various formatting options that can be specified in format strings.
    This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code.
str.index(sub[, start[, end]])功能和find类似,只是当没有发现的时候返回报错
    Like find(), but raise ValueError when the substring is not found.
str.isalnum()判断字符产是英文或数字组成的
    Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise.
    For 8-bit strings, this method is locale-dependent.
str.isalpha()
    Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.
    For 8-bit strings, this method is locale-dependent.
str.isdigit()
    Return true if all characters in the string are digits and there is at least one character, false otherwise.
    For 8-bit strings, this method is locale-dependent.
str.islower()
    Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
    For 8-bit strings, this method is locale-dependent.
str.isspace()
    Return true if there are only whitespace characters in the string and there is at least one character, false otherwise.
    For 8-bit strings, this method is locale-dependent.
str.istitle()
    Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise.
    For 8-bit strings, this method is locale-dependent.
str.isupper()
    Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.
    For 8-bit strings, this method is locale-dependent.
str.join(seq)
    Return a string which is the concatenation of the strings in the sequence seq. The separator between elements is the string providing this method.
str.ljust(width[, fillchar])
    Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s).
    Changed in version 2.4: Support for the fillchar argument.
str.lower()
    Return a copy of the string converted to lowercase. 将字符串转换成小写
    For 8-bit strings, this method is locale-dependent.
str.lstrip([chars])
    Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:
    >>> '   spacious   '.lstrip()
    'spacious   '
    >>> 'www.example.com'.lstrip('cmowz.')
    'example.com'
    Changed in version 2.2.2: Support for the chars argument.
str.partition(sep)
    Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.

New in version 2.5.

str.replace(old, new[, count])
    Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

str.rfind(sub[, start[, end]])
    Return the highest index in the string where substring sub is found, such that sub is contained within s[start,end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.

str.rindex(sub[, start[, end]])
    Like rfind() but raises ValueError when the substring sub is not found.

str.rjust(width[, fillchar])

Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s).

Changed in version 2.4: Support for the fillchar argument.

str.rpartition(sep)

Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself.

New in version 2.5.

str.rsplit([sep[, maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None, any whitespace string is a separator. Except for splitting from the right, rsplit() behaves like split() which is described in detail below.

New in version 2.4.

str.rstrip([chars])

Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:

>>> '   spacious   '.rstrip()
    '   spacious'
    >>> 'mississippi'.rstrip('ipz')
    'mississ'

Changed in version 2.2.2: Support for the chars argument.

str.split([sep[, maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified, then there is no limit on the number of splits (all possible splits are made).

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

For example, ' 1  2   3  '.split() returns ['1', '2', '3'], and '  1  2   3  '.split(None, 1) returns ['1', '2   3  '].

str.splitlines([keepends])
    Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.

str.startswith(prefix[, start[, end]])

Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.

Changed in version 2.5: Accept tuples as prefix.

str.strip([chars])

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

>>> '

python 之 string() 模块的更多相关文章

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

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

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

    http://blog.chinaunix.net/uid-25992400-id-3283846.html http://blog.csdn.net/xiaoxiaoniaoer1/article/ ...

  3. Python之string模块(详细讲述string常见的所有方法)

    相信不少学习python的程序员都接触过string模块 string模块主要包含关于字符串的处理函数 多说无益,初学python的小伙伴还不赶紧码起来 接下来将会讲到字符串的大小写.判断函数. 以及 ...

  4. Python的string模块

    如果要使用string模块,需要先导入该模块 import string string.ascii_lowercase  #打印所有的小写字母 string.ascii_uppercase  #打印所 ...

  5. python 字符串 string模块导入及用法

    字符串也是一个模块,有自己的方法,可以通过模块导入的方式来调用 1,string模块导入 import string 2,  其用法 string.ascii_lowercase string.dig ...

  6. python(string 模块)

    1.string 模块下关键字源码定义 whitespace = ' \t\n\r\v\f' ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' ascii_ ...

  7. python中string模块

    >>> import string >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL ...

  8. python之string模块常量:数字,26个字母,标点符号,空白

    In [8]: import string In [9]: dir(string) In [10]: string.ascii_letters Out[10]: 'abcdefghijklmnopqr ...

  9. python基础===string模块常量

    In [8]: import string In [9]: dir(string) In [10]: string.ascii_letters Out[10]: 'abcdefghijklmnopqr ...

随机推荐

  1. 我进行jvm内存调优的一些记录

    jvm内存调优的一些记录 java内存调优的方法和过程 可以使用 jmap -heap pid号 查看,例如pid是9300,执行的结果可能是这样的. root@ubuntu:~# jmap -hea ...

  2. angularjs学习第二天笔记---过滤器

    您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...

  3. 【16】命令模式(Command Pattern)

    一.前言 最近项目中发现,对于设计模式的了解是必不可少的,当然对于设计模式的应用那更是重要,可以说是否懂得应用设计模式在项目中是衡量一个程序员的技术水平,因为对于一个功能的实现,高级工程师和初级工程师 ...

  4. 查看centos操作系统、java_jdk、hadoop位数

    1.centos操作系统位数: file /bin/ls 此为64位 2.java jdk 位数: java -version 这个是64为的,若是没有,则默认为32位 3.hadoop位数: 进入h ...

  5. python-桥接模式

    源码地址:https://github.com/weilanhanf/PythonDesignPatterns 说明: 有些类在功能设计上要求,自身包含两个或两个以上变化的因素,即该类在二维或者多维上 ...

  6. python 递归和二分法

    一 内置函数 1. revserd 翻转,返回的是迭代器 # 将 s 倒置 s = '不是上海自来水来自海上' # 方法一 print(s[::-1]) # 方法二 s1 = reversed(s) ...

  7. js-jQuery性能优化(二)

    5.数组方式使用jQuery对象 使用jQuery选择器获取结果是一个jQuery对象.然而,jQuery类库会让你感觉正在使用一个定义了索引和长度的数组.在性能方面,建议使用简单的for或者whil ...

  8. H5 canvas 实现飞机大战游戏

    首先看几张效果图: 上面三张图分别对应游戏的三种状态 ready,play,pause.体验一下 先介绍一下canvas 画图的原理,在这个游戏中的背景,飞机,子弹以及飞机被击中爆炸的效果都是一张张的 ...

  9. 纯小白入手 vue3.0 CLI - 2.4 - 新组件 Forms.vue 中学习表单

    vue3.0 CLI 真小白一步一步入手全教程系列:https://www.cnblogs.com/ndos/category/1295752.html 我的 github 地址 - vue3.0St ...

  10. Vue + WebPack + Typescript初学者VSCode项目 (按需加载、跨域调试、await/async)

    万事开头难,一个好的Hello World程序可以节省我们好多的学习时间,帮助我们快速入门.Hello World程序之所以是入门必读必会,就是因为其代码量少,简单易懂.但我觉得,还应该做到功能丰富, ...