Python 第三章 字符串

字符串

  • 所有标准序列操作(索引、切片、乘法、成员资格检查、长度、最小值和最大值)都适用于字符串,但字符串是不可变的,因此所有的元素赋值和切片赋值都是非法的。
    # 字符串不可改变
    >>> website = 'http://www.python.org' 
    >>> website[-3:] = 'com' 
    Traceback (most recent call last): 
     File "<pyshell#19>", line 1, in ? 
     website[-3:] = 'com' 
    TypeError: object doesn't support slice assignment
    
  • 将值转换为字符串并设置其格式是一个重要的操作。
  • 老办法:使用字符串格式设置运算符——百分号。
  • 在%左边指定一个字符串(格式字符串),并在右边指定要设置其格式的值。指定要设置其格式的值时,可使用单个值(如字符串或数字),可使用元组(如果要设置多个值的格式),还可使用字典(这将在下一章讨论),其中最常见的是元组。
  • 老办法了,现在不怎么用,以前用的多。
    >>> format = "Hello, s. s enough for ya?" % %
    >>> values = ('world', 'Hot') 
    >>> format % values 
    'Hello, world. Hot enough for ya?'
    
  • 上述格式字符串中的%s称为转换说明符,指出了要将值插入什么地方。s意味着将值视为字符串进行格式设置。
  • 如果指定的值不是字符串,将使用str将其转换为字符串。
  • %.3f将值的格式设置为包含3位小数的浮点数。
  • 新办法:使用字符串方法format。
  • 使用这种方法时,每个替换字段都用花括号括起,其中可能包含名称,还可能包含有关如何对相应的值进行转换和格式设置的信息。
    >>> "{}, {} and {}".format("first", "second", "third") 
    'first, second and third' 
    >>> "{3} {0} {2} {1} {3} {0}".format("be", "not", "or", "to") 
    'to be or not to be'
    >>> from math import pi 
    >>> "{name} is approximately {value:.2f}.".format(value=pi, name="π") 
    'π is approximately 3.14.'
    
  • 关键字参数的排列顺序无关紧要。在这里,我还指定了格式说明符.2f,并使用冒号将其与字段名隔开。它意味着要使用包含2位小数的浮点数格式。
  • 在Python 3.6中,如果变量与替换字段同名,还可使用一种简写。在这种情况下,可使用f字符串——在字符串前面加上f。
    >>> name = 'wmd'
    >>> f'I am {name}'
    'I am wmd'
    
  • 要在最终结果中包含花括号,可在格式字符串中使用两个花括号(即{{或}})来指定。
    >>> "{{ceci n'est pas une replacement field}}{}".format('over') 
    "{ceci n'est pas une replacement field}over"
    
  • 将按顺序将字段和参数配对。可以给参数指定名称,这种参数将被用于相应的替换字段中。也可以混合使用这两种方法。
    >>> "{foo} {} {bar} {}".format(1, 2, bar=4, foo=3) 
    '3 1 4 2'
    
  • 还可通过索引来指定要在哪个字段中使用相应的未命名参数,这样可不按顺序使用未命名参数。
  • 不能同时使用手工编号和自动编号,因为这样很快会变得混乱不堪。
    >>> "{foo} {1} {bar} {0}".format(1, 2, bar=4, foo=3) 
    '3 2 4 1'
    
  • 也可以访问其组成部分。
    >>> fullname = ["Alfred", "Smoketoomuch"] 
    >>> "Mr {name[1]}".format(name=fullname) 
    'Mr Smoketoomuch' 
    >>> import math 
    >>> tmpl = "The {mod.__name__} module defines the value {mod.pi} for π" 
    >>> tmpl.format(mod=math) 
    'The math module defines the value 3.141592653589793 for π'
    
  • 指定要在字段中包含的值后,就可添加有关如何设置其格式的指令了。
    >>> print("{pi!s} {pi!r} {pi!a}".format(pi="π")) 
    π 'π' '\u03c0'
    
  • 函数str通常创建外观普通的字符串版本(这里没有对输入字符串做任何处理)。函数repr尝试创建给定值的Python表示(这里是一个字符串字面量)。函数ascii创建只包含ASCII字符的表示,类似于Python2中的repr。
  • 还可指定要转换的值是哪种类型,更准确地说,是要将其视为哪种类型。例如:提
    供一个整数,但将其作为小数进行处理。
    >>> "The number is {num}".format(num=42) 
    'The number is 42' 
    >>> "The number is {num:f}".format(num=42) 
    'The number is 42.000000'
    >>> "The number is {num:b}".format(num=42) 
    'The number is 101010'
    
  • 其中:
  • b 将整数表示为二进制数
  • c 将整数解读为Unicode码点
  • d 将整数视为十进制数进行处理,这是整数默认使用的说明符
  • e 使用科学表示法来表示小数(用e来表示指数)
  • E 与e相同,但使用E来表示指数
  • f 将小数表示为定点数
  • F 与f相同,但对于特殊值(nan和inf),使用大写表示
  • g 自动在定点表示法和科学表示法之间做出选择。这是默认用于小数的说明符,但在默认情况下至少有1位小数
  • G 与g相同,但使用大写来表示指数和特殊值
  • n 与g相同,但插入随区域而异的数字分隔符
  • o 将整数表示为八进制数
  • s 保持字符串的格式不变,这是默认用于字符串的说明符
  • x 将整数表示为十六进制数并使用小写字母
  • X 与x相同,但使用大写字母
  • % 将数表示为百分比值(乘以100,按说明符f设置格式,再在后面加上%)
  • 设置浮点数(或其他更具体的小数类型)的格式时,默认在小数点后面显示6位小数,并根据需要设置字段的宽度,而不进行任何形式的填充。
  • 数和字符串的对齐方式不同。
    >>> "{num:10}".format(num=3) 
    '         3' 
    >>> "{name:10}".format(name="Bob") 
    'Bob       '
    
  • 精度也是使用整数指定的,但需要在它前面加上一个表示小数点的句点。
    >>> "Pi day is {pi:.2f}".format(pi=pi) 
    'Pi day is 3.14'
    >>> "{pi:10.2f}".format(pi=pi) 
    '      3.14'
    
  • 于其他类型也可指定精度,但是这样做的情形不太常见。
    >>> "{:.5}".format("Guido van Rossum") 
    'Guido'
    
  • 可使用逗号来指出你要添加千位分隔符。
  • 同时指定其他格式设置元素时,这个逗号应放在宽度和表示精度的句点之间。
    >>> 'One googol is {:,}'.format(10**100) 
    'One googol is 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00 
    0,000,000,000,000,000,000,000,000,000,000,000,000,000,000'
    
  • 在指定宽度和精度的数前面,可添加一个标志。这个标志可以是零、加号、减号或空格,其中零表示使用0来填充数字。
    >>> '{:010.2f}'.format(math.pi)
    '0000003.14'
    >>> '{:+10.2f}'.format(math.pi)
    '     +3.14'
    >>> '{:-10.2f}'.format(math.pi)
    '      3.14'
    >>> '{:-10.2f}'.format(-1 * math.pi)
    '     -3.14'
    >>> '{:-010.2f}'.format(-1 * math.pi)
    '-000003.14'
    
  • 要指定左对齐、右对齐和居中,可分别使用<、>和^。
    >>> print('{0:<10.2f}\n{0:^10.2f}\n{0:>10.2f}'.format(pi)) 
    3.14 
      3.14 
         3.14
    
  • 说明符=指定将填充字符放在符号和数字之间。
    >>> print('{0:10.2f}\n{1:10.2f}'.format(pi, -pi)) 
     3.14 
     -3.14 
    >>> print('{0:10.2f}\n{1:=10.2f}'.format(pi, -pi)) 
     3.14 
    - 3.14
    
  • 井号(#)选项将触发另一种转换方式,转换细节随类型而异。
    >>> "{:b}".format(42) 
    '101010' 
    >>> "{:#b}".format(42) 
    '0b101010'
    >>> "{:g}".format(42) 
    '42' 
    >>> "{:#g}".format(42) 
    '42.0000'
    
  • 例子
    width = int(input('Please enter width: '))
    price_width = 10
    item_width = width - price_width
    
    # {}是从里往外代的。{{}}变成一个{}
    header_fmt = '{{:{}}}{{:>{}}}'.format(item_width, price_width)
    fmt        = '{{:{}}}{{:>{}.2f}}'.format(item_width, price_width)
    
    print('=' * width)
    
    
    
    print(header_fmt.format('Item', 'Price'))
    
    print('-' * width)
    
    print(fmt.format('Apples', 0.4))
    print(fmt.format('Pears', 0.5))
    print(fmt.format('Cantaloupes', 1.92))
    print(fmt.format('Dried Apricots (16 oz.)', 8))
    print(fmt.format('Prunes (4 lbs.)', 12))
    
    print('=' * width)
    
  • 结果:
    Please enter width: >? 35
    ===================================
    Item                          Price
    -----------------------------------
    Apples                         0.40
    Pears                          0.50
    Cantaloupes                    1.92
    Dried Apricots (16 oz.)        8.00
    Prunes (4 lbs.)               12.00
    ===================================
    
  • 方法center()通过在两边添加填充字符(默认为空格)让字符串居中。
    >>> "The Middle by Jimmy Eat World".center(39) 
    ' The Middle by Jimmy Eat World ' 
    >>> "The Middle by Jimmy Eat World".center(39, "*") 
    '*****The Middle by Jimmy Eat World*****'
    
  • 方法strip()将字符串开头和末尾的空白(但不包括中间的空白)删除,并返回删除后的结果。
    >>> ' internal whitespace is kept '.strip() 
    'internal whitespace is kept'
    
  • 方法find(),在字符串中查找子串。如果找到,就返回子串的第一个字符的索引,否则返回-1。
  • 字符串方法find返回的并非布尔值。如果find像这样返回0,就意味着它在索引0处找到了指定的子串。
    >>> 'With a moo-moo here, and a moo-moo there'.find('moo') 
    7 
    >>> title = "Monty Python's Flying Circus" 
    >>> title.find('Monty') 
    0 
    >>> title.find('Python')
    6 
    >>> title.find('Flying') 
    15 
    >>> title.find('Zirquss') 
    -1
    
  • 还可指定搜索的起点和终点(它们都是可选的)。
  • 起点和终点值(第二个和第三个参数)指定的搜索范围包含起点,但不包含终点。
    >>> subject = '$$$ Get rich now!!! $$$' 
    >>> subject.find('$$$') 
    0 
    >>> subject.find('$$$', 1) # 只指定了起点
    20 
    >>> subject.find('!!!') 
    16 
    >>> subject.find('!!!', 0, 16) # 同时指定了起点和终点
    -1
    
  • join()是一个非常重要的字符串方法,其作用与split相反,用于合并序列的元素。
    >>> seq = [1, 2, 3, 4, 5] 
    >>> sep = '+' 
    >>> sep.join(seq) # 尝试合并一个数字列表
    Traceback (most recent call last): 
     File "<stdin>", line 1, in ? 
    TypeError: sequence item 0: expected string, int found 
    >>> seq = ['1', '2', '3', '4', '5'] 
    >>> sep.join(seq) # 合并一个字符串列表
    '1+2+3+4+5' 
    >>> dirs = '', 'usr', 'bin', 'env' 
    >>> '/'.join(dirs) 
    '/usr/bin/env' 
    >>> print('C:' + '\\'.join(dirs)) 
    C:\usr\bin\env
    
  • split()是一个非常重要的字符串方法,其作用与join相反,用于将字符串拆分为序列。
    >>> '1+2+3+4+5'.split('+') 
    ['1', '2', '3', '4', '5'] 
    >>> '/usr/bin/env'.split('/') 
    ['', 'usr', 'bin', 'env'] 
    >>> 'Using the default'.split() 
    ['Using', 'the', 'default']
    
  • 方法lower()返回字符串的小写版本。
    >>> 'Trondheim Hammer Dance'.lower() 
    'trondheim hammer dance'
    
    >>> name = 'Gumby' 
    >>> names = ['gumby', 'smith', 'jones'] 
    >>> if name.lower() in names: print('Found it!') 
    ... 
    Found it! 
    
  • 方法replace()将指定子串都替换为另一个字符串,并返回替换后的结果。
    >>> 'This is a test'.replace('is', 'eez') 
    'Theez eez a test'
    
  • 还可在一个字符串参数中指定要删除哪些字符。
  • 这个方法只删除开头或末尾的指定字符,因此中间的星号未被删除。
    >>> '*** SPAM * for * everyone!!! ***'.strip(' *!') 
    'SPAM * for * everyone'
    
  • 方法translate()与replace()一样替换字符串的特定部分,但不同的是它只能进行单字符替换。这个方法的优势在于能够同时替换多个字符,因此效率比replace高。
  • 使用translate前必须创建一个转换表。这个转换表指出了不同Unicode码点之间的转换关系。要创建转换表,可对字符串类型str调用方法maketrans,这个方法接受两个参数:两个长度相同的字符串,它们指定要将第一个字符串中的每个字符都替换为第二个字符串中的相应字符。
    >>> table = str.maketrans('cs', 'kz', ' ') 
    >>> 'this is an incredible test'.translate(table) 
    'thizizaninkredibletezt'
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值