使用字符串
基本字符操作
所有的标准序列操作(索引、分片、乘法、判断成员资格、求长度、取最小值和最大值)对字符串同样适用。但是,字符串都是不可变的。
字符串格式化:精简版
在%左侧放置一个字符串,而右侧放置希望被格式化的值。
str = 'Hello. %s.%s enough for ya?'
values = ('world','Hot')
print str % values
输出:
Hello. world.Hot enough for ya?
str = 'Pi with three decimals: %.3f'
from math import pi
print str % pi
输出:
Pi with three decimals: 3.142
字符串格式化:完整版
格式化操作符的右操作数可以是任意类型。
如果右操作数是元组的话,则其中的每一个元素都会被单独格式化,每个值都需要一个对应的转换说明符。
print '%s plus %s equals %s'%(1,1,2)
输出:
1 plus 1 equals 2
基本的转换说明符包括以下部分。注意,这些项的顺序是至关重要的。
%字符:标记转换说明符的开始。
转换标志:-表示左对齐;+表示在转换值之前要加上正负号;“”(空白字符)表示整数之前保留空格;0表示转换值若位数不够则用0填充。
最小字段宽度(可选):转换后的字符串至少应该具有该值指定的宽度。如果是×,则宽度会从值元组中读出。
点(.)后跟随精度值(可选):如果转换的是实数,精度值就表示出现在小数后的位数。如果转换的是字符串,那么该数字就表示最大的字段宽度。如果数×,那么精度将会从元组中选出。
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对象) |
简单转换
简单的转换只需要写出转换类型,使用起来很简单:
print 'Price of eggs:$%d'% 42
print 'Hexadecimal price of eggs: %x' % 42
from math import pi
print 'Pi: %f'% pi
print 'Very inexact estimate of pi: %i'% pi
print 'Using str: %s'%42L
print 'Using repr: %r'%42L
输出:
Price of eggs:$42
Hexadecimal price of eggs: 2a
Pi: 3.141593
Very inexact estimate of pi: 3
Using str:42
Using repr:42
字段宽度和精度
from math import pi
print '%10f'%pi
print '%10.2f'%pi
print '%.2f'%pi
print '%.5s'%'Guido van Rossum'
print '%.*s'%(5,'Guido van Rossum')
输出:
3.141593
3.14
3.14
Guido
Guido
符号、对齐和用0填充
在字段宽度和精度之前还可以放置一个“标志”,该标志可以是0,加号、减号或空格。0表示数字将会用0进行填充。
# _*_coding:utf8_*_
#使用给定的宽度打印格式化后的价格列表
width = input('Please enter width:')
price_width = 10;
item_width = width-price_width
header_format = '%-*s%*s'
format = '%-*s%*.2f'
print '=' * width
print header_format % (item_width,'Item',price_width,'Price')
print '-' * width
print format %(item_width,'Apples',price_width,0.4)
print format %(item_width,'Pears',price_width,0.5)
print format %(item_width,'Cantaloupes',price_width,1.92)
print format %(item_width,'Dried Apricots (16 oz.)',price_width,8)
print format %(item_width,'Prunes(4 lbs.)',price_width,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
===================================
字符串方法
find:
find方法可以在一个较长的字符串中查找子串。它返回子串所在位置的最左端索引。如果没有找到则返回-1。
print 'with a moo-moo here, and a moo-moo there'.find('moo')
title = 'Monty Python\'s Flying Circus'
print title.find('Monty')
print title.find('Python')
print title.find('Flying')
print title.find('ABC')
输出:
7
0
6
15
-1
# _*_coding:utf8_*_
subject = '$$$ Get rich now!!!$$$'
print subject.find('$$$')
print subject.find('$$$',1)#只提供起点
print subject.find('!!!')
print subject.find('!!!',0,16)#提供起点和结束点
输出:
0
19
16
-1
join:
join方法用来连接序列中的元素。
seq = '12345'
sep = '+'
print sep.join(seq)
输出:
1+2+3+4+5
lower:
lower方法返回字符串的小写字母版。
a='ASDFGHJKL'
print a.lower()
name = 'Gumby'
names = ['gumby','smith','jones']
if name.lower() in names:
print 'Found it'
输出:
asdfghjkl
Found it
replace:
replace方法返回某字符串的所有匹配项均被替换之后得到字符串。
print 'This is a test'.replace('is','eez')
输出:
Theez eez a test
split:
join的逆方法,将字符串分割。
a = '1+2+3+4+5+6+7+8'
print a.split('+')
输出:
['1', '2', '3', '4', '5', '6', '7', '8']
strip:
去除两侧空格。
a = ' 123 '
print a
print a.strip()
输出:
123
123
translate:
translate方法和replace方法一样,可以替换字符串中的某些部分,但是和前者不同的是,translate方法只能处理单个字符。他的优势在于可以同时进行多个替换,有些时候比replace效率高得多。
本章新函数
string.capwords[s[,sep]] | 使用split函数分割字符串s(以sep为分隔符),使用capitalize函数将分割得到的各单词首字母大写,并且使用join函数以sep为分隔符将各单词连接起来 |
atring.maketrans(from,to) | 创建用于转换的转换表 |