zoukankan      html  css  js  c++  java
  • python 字符串

    字符串格式化:

    示例:

    >>> format = 'Hello, %s. %s enough for ya ?'
    >>> values = ('world', 'hot')
    >>> print format % values
    Hello, world. hot enough for ya ?
    >>>

    注意:%s成为转换说明符,他们标记了需要插入转换值的位置,s表示会被格式化字符串。如果现在格式化字符串里面包括百分号,那么必须使用双百分号%%,

    2.格式化浮点数:

    >>> format = "Pi with three decimals: %.3f"
    >>> from math import pi
    >>> print format % pi

    Pi with three decimals: 3.142

    3.元组

    >>> '%s plus %s equals %s' % (1,2,3)
    '1 plus 2 equals 3'

    4.字段宽度和精度

    >>> '%10f' % pi   #宽度10
    ' 3.141593'
    >>> '%10.2f' % pi   #宽度10,精度2
    ' 3.14'
    >>> '%1.2f' % pi
    '3.14'
    >>> '%6.2f' % pi
    ' 3.14'

    可以使 "*"号做为宽度和精度

    >>> '%.*s' % (5,'Guido van Rossum')  #精度为5
    'Guido'
    >>> '%.*s' % (9,'Guido van Rossum')  #精度为9
    'Guido van'
    >>>

    5.符号,对齐和用0填充

      在字段宽度和精度之之前还可以放置一个“”标志“”,该标志可以是零,加号,减号或空格

    >>> '%010.2f' % pi
    '0000003.14'

    >>> '%-10.2f' % pi
    '3.14 '


    >>> '%+10.2f' % pi
    ' +3.14'

    >>> print ('% 5d' %10) + ' ' + ('% 5d' %-10)
    10
    -10

     6.find方法

    find方法可以在一个较长的字符串中查找子串。它返回子串所在位置的最左端的索引,如果没有找到则返回 -1

    示例:

    >>> 'with a moo-moo here, and a moo-moo there'.find('moo')
    7
    >>> 'with a moo-moo here, and a moo-moo there'.find('with')
    0
    >>> 'with a moo-moo here, and a moo-moo there'.find('hello')
    -1
    >>>

    >>> title = "Monty Python's Flying Circus"
    >>> title.find('Monty')
    0
    >>>

    8.join方法

    join方法是非常重要的字符串方法,它是split方法的逆方法,用来连接序列中的元素

    示例:

    >>> seq = ['1','2','3']
    >>> sep = '+'
    >>> sep.join(seq)
    '1+2+3'
    >>>
    >>> dirs = '','usr','bin','env'
    >>> '/'.join(dirs)
    '/usr/bin/env'
    >>>

    注意:需要被连接的序列元素必须是字符串,

    9.lower方法

    该方法返回字符串的小写字母版

    >>> username = 'LiLei'
    >>> username.lower()
    'lilei'
    >>>

    如果想要编写 “不区分大小写”的代码的话,那么这么方法就派上用场了,代码会忽略大小写状态。例如,若果想在列表中查找一个用户名是否存在:列表包含字符串‘gumby’,而用户输入的是‘Gumby’,就找不到了

    示例:

    >>> if 'Gumby' in ['smith','gumby','jones']:print 'Found it'

    >>>

    如果存储的是‘Gumby’而用户输入的是‘gumby’甚至是‘GUMBY’,结果也是一样的。解决方法就是在存储和搜索时的名字都转换成小写:代码如下:

    >>> name='Gumby'
    >>> names = ['gumby','smith']
    >>> if name.lower() in names :
    print 'Found it'

    Found it
    >>>

    10.replace方法

    replace方法返回某字符串的所有匹配项均被替换之后的到的字符串

    >>> 'This is test'.replace('is', 'eez')
    'Theez eez test'
    >>>

    11.split方法

    这是一个非常重要的字符串方法,它是join的逆方法,用来将字符串分割成序列

    >>> '1+2+3+4+5'.split('+')
    ['1', '2', '3', '4', '5']
    >>> '/usr/bin/env'.split('/')
    ['', 'usr', 'bin', 'env']
    >>> '/usr/bin/env'.split()
    ['/usr/bin/env']
    >>>

    注意:如果不提供任何分隔符,程序会把所有空格作为分隔符(空格、制表、换行等)。

    12.strip方法

    strip方法返回去除两侧(不包括内部)空格的字符串:

    >>> '          internal whitespace is kept          '.strip()
    'internal whitespace is kept'

    它和lower方法一起使用的话就可以很方便的对比输入的和存储的值。让我们回到lower部分中的用户名的例子,假设用户在输入名字的时候无意在后边加上了一个空格:

    >>> names = ['gumby', 'smith', 'jones']
    >>> name = 'gumby '
    >>> if name in names:
    print 'Found it'

    >>> if name.strip() in names:
    print 'Found it'

    Found it

    也可以指定需要去除的字符,将他们列为参数即可,代码如下:

    >>> '*** SPAM * for * everyone!!! ***'.strip('*')
    ' SPAM * for * everyone!!! '
    >>> '*** SPAM * for * everyone!!! ***'.strip('*!')
    ' SPAM * for * everyone!!! '
    >>> '*** SPAM * for * everyone!!! ***'.strip(' *!')
    'SPAM * for * everyone'
    >>>

    13.translate方法

    translate方法和replace方法一样,可以替换字符串中的某些部分,但是和前者不同的是,translate方法只处理单个字符。他的优势在于可以同时进行多个替换,有些时候比replace效率高的多。

    代码如下:

    >>> from string import maketrans
    >>> table = maketrans('cs','kz')
    >>> len(table)
    256
    >>> table[97:123]
    'abkdefghijklmnopqrztuvwxyz'
    >>> 'this is an incredible test'.translate(table)
    'thiz iz an inkredible tezt'
    >>>

    注意:

    translate的第二个参数是可选的,这个参数是用来指定需要删除的字符,例如像删除所有的空格,代码如下:

    >>> 'this is an incredible test'.translate(table,' ')
    'thizizaninkredibletezt'
    >>>

    本章的新函数

    函数 描述
    string.capwords(s[, sep]) 使用split函数分割字符串s(以sep为分隔符),使用capitalize函数将分割得到的各单词手写字母大写,并且使用join函数以sep为分隔符将各单词连接起来,
    string.maketrans(from,to) 创建用于转换的转换表
  • 相关阅读:
    jsp 特殊标签
    poj 1753 Flip Game 高斯消元 异或方程组 求最值
    zoj 3155 Street Lamp 高斯消元 异或方程组 求方案数
    poj1222 EXTENDED LIGHTS OUT 高斯消元解异或方程组 模板
    zoj 3930 Dice Notation 模拟
    zoj 3157 Weapon 线段树求逆序对数
    hdu 1242 Rescue BFS+优先队列
    hdu 3466 Proud Merchants 贪心+01背包
    zoj 3689 Digging 贪心+01背包
    hdu 2602 Bone Collector 01背包模板
  • 原文地址:https://www.cnblogs.com/liyanbin/p/7463713.html
Copyright © 2011-2022 走看看