>> print str.center(20) #生成20个字符长度,str排中间 Python stRING" />

python字符串String模块

发布时间:2019-08-25 09:34:29编辑:auto阅读(1494)

    python的string模块


    1.字符串属性方法操作:


    1.>字符串格式输出对齐

    >>> str = "Python stRING" 

     

    >>> print str.center(20)           #生成20个字符长度,str排中间

       Python stRING   

        

    >>> print str.ljust(20)            #生成20个字符长度,str左对齐

    Python stRING      

     

    >>> print str.rjust(20)            #生成20个字符长度,str右对齐

           Python stRING


    2.>大小写转换

    >>> str = "Python stRING"

     

    >>> str.upper()                #转大写

    'PYTHON STRING'

     

    >>> str.lower()                #转小写 

    'python string'

     

    >>> str.capitalize()           #字符串首为大写,其余小写

    'Python string'

     

    >>> str.swapcase()              #大小写对换 

    'pYTHON STring'

     

    >>> str.title()                #以分隔符为标记,首字符为大写,其余为小写

    'Python String'


    3.>字符串条件判断


    >>> str = '01234'

     

    >>> str.isalnum()                #是否全是字母和数字,并至少有一个字符

    True

    >>> str.isdigit()                #是否全是数字,并至少有一个字符

    True      


    >>> str = 'string'

     

    >>> str.isalnum()                  #是否全是字母和数字,并至少有一个字符

    True

    >>> str.isalpha()                  #是否全是字母,并至少有一个字符 

    True

    >>> str.islower()                  #是否全是小写,当全是小写和数字一起时候,也判断为True

    True

     

    >>> str = "01234abcd"

     

    >>> str.islower()                  #是否全是小写,当全是小写和数字一起时候,也判断为True

    True

     

    >>> str.isalnum()                  #是否全是字母和数字,并至少有一个字符

    True

     

    >>> str = ' '

    >>> str.isspace()                  #是否全是空白字符,并至少有一个字符

    True

     

    >>> str = 'ABC'

     

    >>> str.isupper()                  #是否全是大写,当全是大写和数字一起时候,也判断为True

    True

     

    >>> str = 'Aaa Bbb'

     

    >>> str.istitle()                  #所有单词字首都是大写,标题 

    True

     

     

    >>> str = 'string learn'

     

    >>> str.startswith('str')                 #判断字符串以'str'开头

    True

     

    >>> str.endswith('arn')                      #判读字符串以'arn'结尾

    True


    4.>字符串搜索定位与替换


    >>> str='string lEARn'

     

    >>> str.find('z')              #查找字符串,没有则返回-1,有则返回查到到第一个匹配的索引

    -1

     

    >>> str.find('n')              #返回查到到第一个匹配的索引

    4

     

    >>> str.rfind('n')         #返回的索引是最后一次匹配的

    11

     

    >>> str.index('a')         #如果没有匹配则报错 


    Traceback (most recent call last):

      File "<input>", line 1, in <module>

    ValueError: substring not found

      

    >>> str.index("n")      #同find类似,返回第一次匹配的索引值

    4

     

    >>> str.rindex("n")         #返回最后一次匹配的索引值

    11

     

    >>> str.count('a')      #字符串中匹配的次数

    0

    >>> str.count('n')      #同上

    2

     

    >>> str.replace('EAR','ear')        #匹配替换

    'string learn'

     

    >>> str.replace('n','N')

    'striNg lEARN'

     

    >>> str.replace('n','N',1)

    'striNg lEARn'

     

    >>> str.strip('n')          #删除字符串首尾匹配的字符,通常用于默认删除回车符 

       

    'string lEAR' 

       

    >>> str.lstrip('n')        #左匹配 

       

    'string lEARn' 

       

    >>> str.rstrip('n')        #右匹配 

       

    'string lEAR' 

     

    >>> str = " tab"

     

    >>> str.expandtabs()       #把制表符转为空格

    ' tab'

     

    >>> str.expandtabs(2)      #指定空格数

    ' tab'


    5.>字符串编码与解码


    >>> str = "字符串学习"

    >>> str

    '\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe5\xad\xa6\xe4\xb9\xa0'

     

    >>> str.decode('utf-8')                                #解码过程,将utf-8解码为unicode

    u'\u5b57\u7b26\u4e32\u5b66\u4e60'

     

    >>> str.decode("utf-8").encode('gbk')                      #编码过程,将unicode编码为gbk

    '\xd7\xd6\xb7\xfb\xb4\xae\xd1\xa7\xcf\xb0'

     

    >>> str.decode('utf-8').encode('utf-8')                        #将unicode编码为utf-8 

    '\xe5\xad\x97\xe7\xac\xa6\xe4\xb8\xb2\xe5\xad\xa6\xe4\xb9\xa0'


    6.>字符串分割变换


    >> str = "Learn string"

     

    >>> '-'.join(str)


    'L-e-a-r-n- -s-t-r-i-n-g'

     

    >>> li = ['Learn','string']

     

    >>> '-'.join(li)


    'Learn-string'

     

    >>> str.split('n')

    ['Lear', ' stri', 'g']

     

    >>> str.split('n',1)

    ['Lear', ' string']

     

    >>> str.rsplit('n')

    ['Lear', ' stri', 'g']

     

    >>> str.rsplit('n',1)

    ['Learn stri', 'g']

     

    >>> str.splitlines()

    ['Learn string']

     

    >>> str.partition('n')

    ('Lear', 'n', ' string')

     

    >>> str.rpartition('n')

    ('Learn stri', 'n', 'g')




    1.    string.atof(s) 字符串转换成浮点型


    string.atof('1.11')

    输出结果:1.11

    string.atof('1')

    输出结果:1.0

     

    2.     string.atoi(s[, base]) 字符串转换成整型


    string.atoi('11') or string.atoi('11', 10)

    输出结果:11

    string.atoi('11', 2)

    输出结果:3

    string.atoi('11', 8)

    输出结果:9

    string.atoi('11', 16)

    输出结果:17

     

    3.     string.capitalize(s) 字符串的第一个字符转换成大写


    string.capitalize('hello world')

    输出结果:Hello world

     

    4.     string.capwords(s[, sep]) 字符串以sep为分隔符分割后的每个字段的首位转换为大写


    string.capwords('hello world')

    输出结果:Hello World

    string.capwords('hello world', 'l')

    输出结果:HellO worlD

     

    5.     string.center(s, len[, fillchar])字符串转换成指定长度,不够的用fillchar补充,且补充的字符在两边

    string.center('hello world', 10, '*')

    输出结果:hello world

    string.center('hello world', 15, '*')

    输出结果:**hello world**

     

    6.     string.count(s, sub[, start[, end]])查询sub在s中的个数

    string.count('hello world', 'l')

    输出结果:3

    string.count('hello world', 'l', 3)

    输出结果:2

    string.count('hello world', 'l', 3, 6)

    输出结果:1

    7.     string.find(s, sub[, start,[end]]) 查询sub在s中的第一个位置

    string.find('hello world', 'l')

    输出结果:2

    string.find('hello world', 'l', 4, 6)

    输出结果:-1

     

    8.     string.ljust(s, len[, fillchar])字符串左对齐,不够用fillchar补充

    string.ljust('hello world', 15)

    输出结果:hello world

    string.ljust('hello world', 15, '*')

    输出结果:hello world****

     

    9.     string.lstrip(s[, chars]) 清除左边的空白字符

    string.lstrip(' hello world')

    输出结果:hello world

    string.lstrip('hello world', 'h')

    输出结果:ello world

     

    10.    string.upper(s) 字符串转换成大写的

    string.upper('hello world')

    输出结果:HELLO WORLD

     

    11.    string.join(list[, sep]) list里的字符串用sep连接起来

    string.join(['hello', 'world'])

    输出结果:hello world

    string.join(['hello', 'world'], '*')

    输出结果:hello*world

     

    12.    string.replace(s, old, new[,max]) 字符串s里的old替换为new,最多替换为max次

    string.replace('hello world', 'l', 'L')

    输出结果:heLLo worLd

    string.replace('hello world', 'l', 'L', 1)

    输出结果:heLlo world

     

    13.    string.translate(s, table[,delchar]) 字符串转换为指定的table里的字符,且删除指定的delchar

    table = string.maketrans('hello', 'HELLO')

    string.translate('hello world', table)

    输出结果:HELLO wOrLd

    string.translate('hello world', table, 'l')

    输出结果:HEO wOrd

     

    14.    string.split(s[, sep[,maxsplit]])  字符串以sep作为分隔符,maxsplit作为分隔次数进行分隔

    string.split('hello world')


    输出结果:['hello', 'world']


    string.split('hello world', 'l')


    输出结果:['he', '', 'o wor', 'd']


    string.split('hello world', 'l', 1)


    输出结果:['he', 'lo world']


    模板

    map = {'var': 'hello world'}

    tmp = string.Template('my first output:${var}')

    tmp.substitute(map)

    输出结果:my first output: hello world

    tmp.safe_substitute(map)

    输出结果:同上

     

    map = {'var': 'hello world'}

    tmp = string.Template('my first output:${vars}')

    tmp.substitute(map)

    输出结果:

    tmp.safe_substitute(map)

    输出结果:my first output: ${vars}




    1 基本字符串操作

    说明:字符串也是序列的一种,所以分片,乘法,索引,求长度,最大, 最小,判断成员资格等都可以应用在字符串上;

    注意:字符串是不可变的,所以不能对其进行赋值;

    例子

    1:  >>> mystr="Test string"


    2:  >>> mystr[0] = 't'


    3:  Traceback (most recent call last):


    4:  File "<pyshell#1>", line 1, in <module>


    5:    mystr[0] = 't'


    6:  TypeError: 'str' object does not support item assignment


    7:  >>>


    2 字符串格式化:精简版

     

    2.1 用字符串格式化操作符


    说明:字符串格式化使用字符串格式化操作符百分号( % )实现,在操作符的左侧是格式化字符串,右侧是希望被格式化的值;

    注意:

    只有元组和字典可以被格式化为一个以上的值,列表和其他序列会被格式化为一个值;

    转换说明符,用于标记需要插入转换值的位置;

    如果在格式化字符串中要输出百分号,则需要使用 %%

    例子:

     1:  #一般格式化


     2:  >>> myformat = "Hello, my name is %s %s"


     3:  >>> name = ('Bill','Gunn')


     4:  >>> print (myformat % name)


     5:  Hello, my name is Bill Gunn


     6:  >>>


     7:  


     8:  #用列表格式化


     9:  >>> myformat = 'Hello, my name is %s'


    10:  >>> name=['Bill', 'Gunn']


    11:  >>> print(myformat % name)


    12:  Hello, my name is ['Bill', 'Gunn']


    13:  


    14:  #打印浮点数


    15:  >>> import math


    16:  >>> print ("PI = %.5f" % pi)


    17:  PI = 3.14159


    18:  


    19:  #打印百分号


    20:  >>> print("%.2f%%"% 22.3)


    21:  22.30%


    22:  >>>


    2.2 用string的Template格式化字符串


    说明:类似于Unix Shell中的变量替换,使用substitute方法,将字符串 模板中的$foo替换为传递进来的参数foo

    例子:

     1:  #从string模块中导入Template


     2:  >>> from string import Template


     3:  #创建模板


     4:  >>> myformat = Template("My name is $name")


     5:  #替换变量并打印


     6:  >>> print(myformat.substitute(name="Bill Gunn"))


     7:  My name is Bill Gunn


     8:  >>>


     9:  


    10:  #输出美元符号的方法,在模板里输入两个$


    11:  >>> mytemplate = Template("The price is $$$price")


    12:  >>> mytemplate.substitute(price=100)


    13:  'The price is $100'


    14:  >>>


    15:  


    16:  #如果参数与后面的字符串相连,需要用大括号将其括起来


    17:  >>> from string import Template


    18:  >>> mytemplate = Template("It's ${x}tastic!")


    19:  >>> mytemplate.substitute(x='slum')


    20:  "It's slumtastic!"


    21:  >>>


    22:  


    23:  #使用字典替换参数


    24:  >>> mytemplate = Template("My $property is $value")


    25:  >>> name = {}


    26:  >>> name["property"] = "name"


    27:  >>> name["value"] = "Bill Gunn"


    28:  >>> mytemplate.substitute(name)


    29:  'My name is Bill Gunn'


    30:  >>>


    31:  


    3 字符串格式化:完整版

    说明:字符串格式化操作符的右操作数如果是元组,那么在格式化字符串 中必须将元组中的各个元素都有对应的转义说明符。

    例子:

     1:  >>> data = tuple(list("123"))


     2:  >>> data


     3:  ('1', '2', '3')


     4:  #格式化字符串中只有一个转义说明符,而元组中有三个元素,转换会报错


     5:  >>> print ("data is %s" % data)


     6:  Traceback (most recent call last):


     7:    File "<pyshell#18>", line 1, in <module>


     8:      print ("data is %s" % data)


     9:  TypeError: not all arguments converted during string formatting


    10:  #显示元组中的全部元素


    11:  >>> print ("data is %s %s %s" % data)


    12:  data is 1 2 3


    13:  >>>


    14:  


    3.1 转换说明符


    转换说明符

     

    转义说明符 含义

    d,i 带符号的十进制整数

    o 不带符号的八进制

    u 不带符号的十进制

    x 不带符号的十六进制(小写)

    X 不带符号的十六进制(大写)

    e 科学计数法的浮点数(小写)

    E 科学计数法的浮点数(大写)

    f,F 十进制浮点数

    g 如果指数大于-4或者小于精度值则和e相同,否则和f相同

    G 如果指数大于-4或者小于精度值则和E相同,否则和F相同

    C 单字符(接受整数或者单字符字符串)

    r 字符串(使用repr转换任意Python对象)

    s 字符串(使用str转换任意Python对象)

    3.2 简单转换


    例子:

     1:  #十进制整数


     2:  >>> print ("The price is $%d" % 12)


     3:  The price is $12


     4:  


     5:  #十六进制整数


     6:  >>> print ("Hex %x" % 12)


     7:  Hex c


     8:  


     9:  #八进制整数


    10:  >>> print ("Oct %o" % 12)


    11:  Oct 14


    12:  >>>


    13:  


    3.3 字段宽度和精度


    说明:

    字段宽度:转换后的值所保留的最小字符个数;

    字段精度:转换后,结果中应该的小数位数;

    可以使用*作为字段宽度或者精度

    例子:

     1:  #限制宽度


     2:  >>> "%10f" % math.pi


     3:  '  3.141593'


     4:  


     5:  #限制小数位数


     6:  >>> "%5.2f" % math.pi


     7:  ' 3.14'


     8:  


     9:  #用星号限制宽度和精度,下例中,宽度为10,精度为5


    10:  >>> '%*.*s' % (10, 5, 'adfasdfadsfasdfasdfasdf')


    11:  '     adfas'


    12:  >>>


    13:  


    3.4 符号,对齐和 0 填充


    说明:

    零:宽度不够时用数字0填充;

    负号:左对齐;

    正号:不管是正数还是负数都标记出符号

    空格:宽度不够时用空格填充;

    例子:

     1:  #空白补0


     2:  >>> print ("%010f" % math.pi)


     3:  003.141593


     4:  


     5:  #左对齐


     6:  >>> "%-10.2f" % math.pi


     7:  '3.14      '


     8:  


     9:  #空白右对齐


    10:  >>> print("% 5d\n% 5d" % (123, 12))


    11:    123


    12:     12


    13:  


    14:  #显示正负符号


    15:  >>> print ("%+5d\n%+5d" % (123, -123))


    16:   +123


    17:   -123


    18:  >>>


    19:  


    4 字符串方法

     

    4.1 find


    说明:用于在长字符串中查找子字符串,如果找到,则返回子字符串在左 侧第一次出现的索引,没找到返回-1,在查找时,还可以指定在长字符串 中查找的范围,提供起始索引和结束索引作为查找的参数;

    注意:查找时,包括起始索引位置,但是不包括结束索引的位置;

    例子:

     1:  >>> string.ascii_letters


     2:  'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'


     3:  >>> letters = string.ascii_letters


     4:  >>> letters.find('AB')


     5:  26


     6:  >>> letters.find('X',30,-1)


     7:  49


     8:  >>> letters.find("AB",26)


     9:  26


    10:  


    4.2 join


    说明:将队列中的元素用字符串连接起来,并且列表中的元素必须是字符 串;

    例子:

    1:  >>> data = list('123456')


    2:  >>> data


    3:  ['1', '2', '3', '4', '5', '6']


    4:  >>> "AB".join(data)


    5:  '1AB2AB3AB4AB5AB6'


    6:  >>> 


    7:  


    4.3 lower


    说明:将字符串转换成小写字母,并返回,但是原字符串不改变

    例子:

    1:  >>> mystr="ABCD"


    2:  >>> mystr.lower()


    3:  'abcd'


    4:  >>> mystr


    5:  'ABCD'


    6:  >>> 


    7:  


    4.4 replace


    说明:返回所有匹配项都被替换之后的字符串

    例子:

    1:  >>> mystr = "My name is Geng Qi"


    2:  >>> mystr.replace("Geng Qi", "Bill Gunn")


    3:  'My name is Bill Gunn'


    4:  >>> 


    4.5 split


    说明:将字符串分割成序列;

    注意:如果不提供分割符,则会将空白符当作分割符

    例子

     1:  #以加号为分割符


     2:  >>> mystr = "1+2+3+4+5+6"


     3:  >>> mystr.split('+')


     4:  ['1', '2', '3', '4', '5', '6']


     5:  


     6:  #不提供分割符时,以空白符为分割符


     7:  >>> mystr = "This    is a       test string"


     8:  >>> mystr.split()


     9:  ['This', 'is', 'a', 'test', 'string']


    10:  >>> 


    11:  


    4.6 strip


    说明:去除两侧的空白,也可以去除指定的字符

    例子:

     1:  >>> mystr = "           asdfad adfasf asdf                      "


     2:  >>> mystr


     3:  '     \tasdfad adfasf asdf       \t\t'


     4:  #去除空白符


     5:  >>> mystr.strip()


     6:  'asdfad adfasf asdf'


     7:  


     8:  #去除指定字符


     9:  >>> mystr.strip('\t')


    10:  '     \tasdfad adfasf' asdf       '


    11:  >>> 


    12:  


    4.7 translate


    说明:translate是单字替换,可以同时替换多个字符

    例子:

    1:  >>> table = str.maketrans('cs', 'kz')


    2:  >>> table


    3:  {115: 122, 99: 107}


    4:  >>> "Please don't knock at my door!".translate(table)


    5:  "Pleaze don't knokk at my door!"


    6:  


关键字