09python字符串,这篇文章主要介绍如何


在05python字符串基础中我们已经大致介绍过字符串,知道如何创建字符串,以及如何使用索引和切片来访问字符串中的字符。这篇文章主要介绍如何使用字符串来设置其他值的格式(比如便于打印),并大致了解使用字符串方法可完成的重要任务,如拆分、合并和查找等。

字符串基本操作

字符串是一种序列,所以所有的标准序列操作都适用于字符串

  • 索引
  • 切片
  • 乘法
  • 成员资格检查
  • 长度
  • 最大值
  • 最小值

同时,字符串是一种不可变序列,所以元素赋值和切片赋值都是非法的,这方面字符串的性质更类似于元组。

>>> 
>>> website = 'http://www.python.org'
>>> website[-3:] = 'com'          
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>>

设置字符串的格式

将值转换为字符串并设置其格式是一个重要的操作,Python提供了多种字符串格式设置方法,即字符串格式化。

  • 百分号

通过在 %左边左边指定一个字符串(格式字符串),并在右边指定要设置的其格式的值,可以格式化字符串。指定要设置其格式的值时,可使用单个值(如字符串或数字),可以使用元组(如果要设置多个值格式),还可以使用字典,其中最常见的是元组

>>> 
>>> format = 'Hello, %s. %s enough for you?' # %s表示格式化字符串
>>> values = ('world', 'Hot')
>>> format % values
'Hello, world. Hot enough for you?'
>>>
>>> 'Hello %s. %s enough for you?' % ('world', 'Hot')
'Hello world. Hot enough for you?'
>>>

%s称为转换说明符,指出了要将值插入在什么地方。s表示要将值视为字符串惊醒格式设置。

  • 模板字符串

模板字符串类似于UNIX shell中的语法,旨在简化基本的格式设置机制。

>>> 
>>> tmpl = Template('Hello, $who! $what enough for you?') 
>>> tmpl.substitute(who='Mars', what='Dusty')
'Hello, Mars! Dusty enough for you?'
>>>

包含等好的参数称为关键字参数,在字符串格式设置中,可将关键字参数视视作为一种向命名替换字段提供值得方式。

  • format——最常用的方法

在编写新代码时,应选择使用字符串方法format,使用这种方法时,每个替换字段都用 {}括起来,其中可能包含名称,还可能包含有关如何应对相应的值进行转换和格式设置的信息。

>>> 
>>> '{}, {} and {}.'.format('first', 'second', 'third') 
'first, second and third.'
>>> '{0}, {1} and {2}.'.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.'
>>>
>>> from math import pi
>>> '{name} is approximately {value}.'.format(value=pi, name='Π')   # 格式说明符.2f需要使用:与字段名隔开。如果没有格式说明符,会展示所有小数
'Π is approximately 3.141592653589793.'
  • f格式化

在python3.6中,如果变量与替换字段同名,还可以使用format的一种简写方式:使用f字符串——在字符串前面加上f

>> 
>>> from math import e
>>> f"Euler's constant is roughly {e}."
"Euler's constant is roughly 2.718281828459045."
>>>
>>> "Euler's constant is roughly {e}.".format(e=e)  # 与上面是等价的
"Euler's constant is roughly 2.718281828459045."

format格式化字符串

格式化字符串的内容很多,这一部分主要介绍对字符串调用方法format,并提供要设置其格式的值。基本原理就是给定每个值都被插入到字符串中,以替换用花括号中的替换字段

如果要在最终结果中包含 {},可在格式字符串中使用两个花括号(即{{}})来指定。

>>> 
>>> "{{ceci n'est pas une replacement field}}".format()           
"{ceci n'est pas une replacement field}"
>>>

格式化字符串中,替换字段由以下三部分组成:

  • 字段名:索引或标识符,指出要设置哪个值的格式并使用结果来替换该字段。除指定值外,还可以指定值的特定部分,如列表元素。
  • 转换标志:跟在叹号后面的单个字符。当前支持的字符包括 r(表示repr)s(表示str)b。如果使用了转换标志,将不使用对象本身的格式设置机制,而是使用指定的函数将对象转换为字符串,再做进一步的格式设置。
  • 格式说明符:跟在冒号后面的表达式(这种表达式是使用微型格式指定语言表示的)。格式说明符让我们能够详细地指定最终的格式,包括格式类型(如字符串、浮点数或十六进制数),字段宽度和数的精度,如何显示符号和千位分隔符,以及各种对齐和填充方式。

替换字段名

有3中方式设置替换字段名

  • 最简单的就是向format提供要设置其格式的未命名参数
  • 通过索引来指定要在哪个字段种使用相应的未命名参数
  • 通过访问值的组成部分来使用
>>> 
>>> '{foo} {} {bar} {}'.format(2, 4, bar=3, foo=1) # 第1种方式  
'1 2 3 4'
>>>
>>> '{foo} {1} {bar} {0}'.format(2, 4, bar=3, foo=1)  # 第2种方式  
'1 4 3 2'
>>>
>>> fullname = ['Alex', 'Bob']
>>> 'Mr {name[1]}.'.format(name=fullname)    # 第3种方式
'Mr Bob.'
>>> 
>>> import math
>>> tmpl = 'The {mod.__name__} moudle defines the value {mod.pi} for Π.'
>>> tmpl.format(mod=math)                     # 第3种方式
'The math moudle defines the value 3.141592653589793 for Π.'
>>>

基本转换

>>> 
>>> print('{pi!s} {pi!r} {pi!a}'.format(pi='Π')) 
Π 'Π' '\u03a0'
>>>
>>> 'The number is {num}'.format(num=42)
'The number is 42'
>>> 'The number is {num:.2f}'.format(num=42) # 整数转换为小数
'The number is 42.00'
>>> 'The number is {num:b}'.format(num=42)   # 二进制
'The number is 101010'
>>>

在指定了字段中包含的值后, 就可以通过添加转换标志来设置格式。

  • s-str转换,函数str通常创建外观普通的字符串版本(这里没有对输入字符串做任何处理)
  • r-repr转换,函数repr尝试创建给定值的Python表示(这里是一个字符串字面量)
  • a-ascii转换,函数ascii创建只包含ASCII字符的表示,类似于Python 2中的repr

还可以指定要转换的值是哪种类型,例如可以提供一个整数将其作为小数进行处理。类型说明符:

类型 含义
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       '
>>>
>>> from math import pi
>>> 'Pi day is {pi:.2f}.'.format(pi=pi) # 设置精度
'Pi day is 3.14.'
>>>
>>> from math import pi
>>> '{pi:10.2f}.'.format(pi=pi)  # 设置宽度和精度  
'      3.14.'
>>>
>>> 'One google is {:,}'.format(10**100) # 设置分格符
'One google is 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000'
>>>

符号、对其和用0填充

设置完精度和宽度后,基本上就能满足打印的需要,但是数字里面有负号会打乱格式,所以需要对符号进行处理。

另外字符串和数字的默认对齐方式不同,也需要处理。

在指定宽度和精度的数据面前,可以添加一个标志,可以是零、加号、减号、空格等,其中使用0来填充数字。

可以使用填充字符来扩充对齐说明符,而不是默认的空格。

等号指定将填充字符放在符号和数字之间。

给正数加上符号使用说明符+。

井号放在说明符和宽度之间会触发一种转换方式,进制会加上前缀。


>>> 
>>> from math import pi   
>>> 
>>> '{:010.2f}.'.format(pi) # 指定 填充、长度、小数位数
'0000003.14.'
>>> 
>>> '{0:<10.2f}.'.format(pi) # 左对齐
'3.14      .'
>>>
>>> '{0:>10.2f}.'.format(pi) # 右对齐
'      3.14.'
>>>
>>> '{0:^10.2f}.'.format(pi)  # 居中对齐
'   3.14   .'
>>> 
>>> '{:$^15}.'.format('  WIN BIG  ')      # 字符填充
'$$  WIN BIG  $$.'
>>> 
>>> 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
>>>
>>> 
>>> print('{0:-.2}\n{1:-.2}'.format(pi, -pi))   # 默认设置
3.1
-3.1
>>> print('{0:+.2}\n{1:+.2}'.format(pi, -pi))  # +放在说明符后面给正数加上符号
+3.1
-3.1
>>> print('{0: .2}\n{1: .2}'.format(pi, -pi))  # 说明符指定空格在正数前面加上空格
 3.1
-3.1
>>>
>>> 
>>> '{:b}'.format(42) 
'101010'
>>> '{:#b}'.format(42)  # #在进制转换会加上前缀
'0b101010'
>>>
>> 
>>> '{:g}'.format(42)  
'42'
>>> '{:#g}'.format(42) # 对于各种十进制数,它要求必须包含小数点(对于类型g,它保留小数点后面的零)
'42.0000'
>>>

设置字符串格式示例:

# 根据指定的宽度打印格式良好的价格列表

width = int(input('PLease enter width: '))

prince_width = 10
item_width = width - prince_width

header_fmt = '{{:{}}}{{:>{}}}'.format(item_width, prince_width)
fmt = '{{:{}}}{{:>.2{}}}'.format(item_width, prince_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.0))
print(fmt.format('Prunes (4 lbs.)', 12.2))

print('=' * width)


### Output
PLease enter width: 35
===================================
Item                          Price
-----------------------------------
Apples                   0.40000000000000002220446049250313080847263336181640625
Pears                    0.5
Cantaloupes              1.9199999999999999289457264239899814128875732421875
Dried Apricots (16 oz.)  8.0
Prunes (4 lbs.)          12.199999999999999289457264239899814128875732421875
===================================

字符串方法

字符串的方法比列表多很多,都是从string那里“继承”过来的,所以strings模块中包含了很多字符串没有的常量和函数。

我们这这里主要介绍一些字符串一些常用的方法:

  • center

方法center通过在两边添加填充字符(默认位空格)让字符串居中。

>>> 
>>> 'Must to study python.'.center(30) 
'    Must to study python.     '
>>>
>>> 'Must to study python.'.center(30, '*')  # 中间对齐
'****Must to study python.*****'
>>> 
>>> 'Must to study python.'.ljust(30, '*')  # 左对齐
'Must to study python.*********'
>>> 'Must to study python.'.rjust(30, '*')  # 右对齐
'*********Must to study python.'
>>> 'Must to study python.'.zfill(30)  
'000000000Must to study python.'
>>>
  • find

方法find在字符串中查找子串。如果找到,就返回子串的第一个字符索引,否则返回-1。注意find的返回结果并非布尔值!!

>>> 
>>> my_str = 'With a moo-moo here, and a moo-moo there'
>>> my_str.find('moo') 
7
>>>
>>> my_str = 'Monty Python\'s Flying Circus'  
>>> my_str.find('Monty') 
0
>>> my_str.find('Python') 
6
>>> my_str.find('python') 
-1
>>>
>>> my_str = '$$$ Get rich now!!! $$$'  # 还可以用作查找特殊字符  
>>> my_str.find('$$$')  
0
>>>
>>> my_str.find('$$$', 1)   # find还可以指定起点,注意这里的起点和终点都是指字符串的索引
20
>>> my_str.find('!!!')  
16
>>> my_str.find('!!!', 0, 16)  # 同时指定起点和终点:搜索范围包括起点,但是不包括终点————这也是Python一贯的做法。。。
-1
>>>
>>> my_str.rfind('$$$')  # 从右边开始找
20
>>>
>>> my_str = '$$$ !!!Get rich now!!! $$$'
>>> my_str.index('!!!')  # 字符串索引
4
>>> my_str.rindex('!!!') # 右边字符串索引
19
>>> my_str.count('!!!')  # 统计
2
>>> my_str.startswith('!!!')  # 开始字符串,boolen
False
>>> my_str.endswith('$$$') # 结束字符串,boolen
True
>>>
  • join——重点掌握

方法join用于合并序列元素,与split作用相反

>>> 
>>> seq = [1, 2, 3, 4]
>>> sep = '+'
>>> sep.join(seq) # 尝试合并一个数字列表,会报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found
>>> seq = ['1', '2', '3', '4']
>>> sep.join(seq) # 合并一个字符串列表,正常
'1+2+3+4'
>>>
>>>
>>> dirs = '', 'usr', 'bin', 'env'
>>> '/'.join(dirs) 
'/usr/bin/env'
>>> print('C:' + '\\'.join(dirs))
C:\usr\bin\env
>>>
>>> 
>>> my_str = r'D:\usr\bin\env'
>>> my_str.split('\\')            # split用于分隔字符串,返回一个列表
['D:', 'usr', 'bin', 'env']
>>>
  • lower

方法lower返回字符串的小写版本。

>>
>>> my_str = 'Trondheim Hammer Dance'
>>> my_str.lower()
'trondheim hammer dance'
>>> my_str.lower().title()  # title方法将每个单词的首字母大写
'Trondheim Hammer Dance'
>>>

可以在检查字符串是否包含时,全部转换为大写或者小写,这样会很方便

  • replace——重点掌握

方法replace将指定字符串都替换为另一个字符串,并返回替换后的结果。

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

类似文字软件中的“查找并替换”功能。

  • split——重点掌握

split是一个非常重要的字符串方法,用于将字符串拆分为序列。其作用与join相反。

>>> 
>>> '1 + 2 + 3 + 4'.split('+') 
['1 ', ' 2 ', ' 3 ', ' 4']
>>> 'Using the default'.split() # 如果没有指定分隔符,将默认在单个或多个连续的空白字符(空格、制表符、换行符)处进行拆分。
['Using', 'the', 'default']
>>>
  • strip

方法strip将字符串开头和末尾的空白(但不包括中间的空白)删除,并返回删除后的结果。

>>> 
>>> '     hello world    '.strip()
'hello world'
>>>
>>> '*** SPAM * for * everyone!!! ***'.strip('*!')   # 可以指定删除开头或末尾的字符
' SPAM * for * everyone!!! '
>>>
>>> 
>>> names = ['gumby', 'smiith', 'jones'] 
>>> name = 'Gumby ' 
>>> if name in names: print('Found it!')
... 
>>> if name.lower() in names: print('Found it!')
... 
>>> if name.strip() in names: print('Found it!')
... 
>>> if name.lower().strip() in names: print('Found it!') # 用于查找字符串的时候很方便
... 
Found it!
>>>

与lower一样,需要将输入与存储的值进行比较时,strip很有用。

  • translate——重点掌握

方法translate与replace一样替换字符串的特定部分,但不同的是,只进行单字符替换。优势在于能过同时替换多个字符,效率比replace高。translate替换的原理是使用maketrans方法创建了一个转换表,表明了要替换字符之间的转换关系(展示的是Unicode码点之间的映射)。

>>>
>>> table = str.maketrans('cs', 'kz') # 第一个参数是需要替换的字符,第二个参数是替换后的字符
>>> table
{99: 107, 115: 122}
>>>
>>> 'This is an incredible test'.translate(table)   
'Thiz iz an inkredible tezt'
>>> 
>>>
>>> table = str.maketrans('cs', 'kz', ' ') # 第三个参数指定要删除的字符
>>> table
{99: 107, 115: 122, 32: None}
>>> 'This is an incredible test'.translate(table)   
'Thizizaninkredibletezt'
  • 判断字符串是否满足特定条件

很多字符串的方法都以 is开头,它们判断字符串是否具有特定的性质。如果具有该性质,返回True,否则返回False。

>>> 
>>> my_var = 'Learning Python'
>>> my_var.isupper()
False
>>> my_var.islower() 
False
>>> my_var.istitle() 
True
>>>

评论关闭