分类: Python/Ruby
2009-08-16 14:47:31
磁针石:xurongzhong#gmail.com
支持的序列的基本操作:(indexing, slicing, multiplication, membership, length,
minimum, and maximum)
格式化:
>>> format = "Hello, %s. %s enough for ya?"
>>> values = ('world', 'Hot')
>>> print format % values
Hello, world. Hot enough for ya?
注意,除了数组和字典外,其他的序列都解释为单个值。
真正的百分号要用%%表示。
其他类型:
>>>
format = "Pi with three decimals: %
>>> from math import pi
>>> print format % pi
Pi with three decimals: 3.142
另外一种类似shell的格式化方法:TEMPLATE STRINGS
>>> from string import Template
>>> s = Template('$x, glorious $x!')
>>> s.substitute(x='slurm')
>>> s = Template("It's ${x}tastic!")
>>> s.substitute(x='slurm')
"It's slurmtastic!"
插入美元符号:
>>> s = Template("Make $$ selling $x!")
>>> s.substitute(x='slurm')
'Make $ selling slurm!'
配合字典的使用:
>>> s = Template('A $thing must never $action.')
>>> d = {}
>>> d['thing'] = 'gentleman'
>>> d['action'] = 'show his socks'
>>> s.substitute(d)
'A gentleman must never show his socks.'
长版本的字符串格式化:
>>> '%s plus %s equals %s' % (1, 1, 2)
'1 plus 1 equals 2'
>>> 'Price of eggs: $%d' % 42
'Price of eggs: $42'
>>> 'Hexadecimal price of eggs: %x' % 42
'Hexadecimal price of eggs:
>>> from math import pi
>>> 'Pi: %f...' % pi
'Pi: 3.141593...'
>>> 'Very inexact estimate of pi: %i' % pi
'Very inexact estimate of pi: 3'
>>> 'Using str: %s' %
'Using str: 42'
>>> 'Using repr: %r' %
'Using repr:
具体介绍参见教材84页
>>> '%
' 3.14'
>>> '%
'3.14'
>>> '%.5s' % 'Guido van Rossum'
'Guido'
>>> '%.*s' % (5, 'Guido van Rossum')
'Guido'
>>> '%
'0000003.14'
注意010在这里不是代表8进制。
>>> '%
'3.14
在正数前面添加空格
>>> print ('% 5d' % 10) + '\n' + ('% 5d' % -10)F
10
-10
正数前添加“+”号。
>>> print ('%+5d' % 10) + '\n' + ('%+5d' % -10)
+10
-10
字符串格式化实例:
# Print a formatted price list with a given width
width = input('Please enter width: ')
price_width = 10
item_width = width - price_width
header_format = '%-*s%*s'
format = '%-*s%*
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
(
print format % (item_width, 'Prunes (
print '=' * width
运行结果:
Please enter width: 35
===================================
Item Price
———————————————————————————————————
Apples 0.40
Pears 0.50
Cantaloupes 1.92
Dried Apricots (
Prunes (
字符串常量:
? string.digits: A string containing the
digits 0–9
? string.letters: A string containing all
letters (uppercase and lowercase)
? string.lowercase: A string containing all
lowercase letters
? string.printable: A string containing all
printable characters
? string.punctuation: A string containing
all punctuation characters
? string.uppercase: A string containing all
uppercase letters
这个东东跟所在区域有关的,比如:string.ascii_letters
查找:
>>> 'With a moo-moo here, and a
moo-moo there'.find('moo')
7
如果没有查找到,返回-1.
>>> subject = '$$$ Get rich now!!!
$$$'
>>> subject.find('$$$')
0
>>> subject.find('$$$', 1) # Only
supplying the start
20
1表示查找的起点。
连接:
>>>
seq = ['1', '2', '3', '4', '5']
>>> sep.join(seq) # Joining a list
of strings
'1+2+3+4+5'
>>> dirs = '', 'usr', 'bin', 'env'
>>> '/'.join(dirs)
'/usr/bin/env'
>>> print 'C:' + '\\'.join(dirs)
C:\usr\bin\env
小写:
lower:
>>> 'Trondheim Hammer
Dance'.lower()
'
相关的有:See also:
translate.
In Appendix B: islower, capitalize,
swapcase, title, istitle, upper, isupper.
>>> "that's all
folks".title()
"That'S All, Folks"
>>> import string
>>> string.capwords("that's
all, folks")
"That's All, Folks"
替换:
>>> 'This is a test'.replace('is',
'eez')
'Theez eez a test
切割:split
>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
去掉多余的空格:strip
>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
>>> '*** SPAM * for * everyone!!!
***'.strip(' *!')
'SPAM * for * everyone'
替换单个字符:translate:暂不涉及