Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19912799
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2009-08-16 14:47:31

字符串

 

2009-8-16

磁针石:xurongzhong#gmail.com

博客:oychw.cublog.cn

§3.1  基本的字符串操作

 

支持的序列的基本操作:(indexing, slicing, multiplication, membership, length,

minimum, and maximum)

 

§3.2  字符串格式化的短格式

 

格式化:

>>> format = "Hello, %s. %s enough for ya?"

>>> values = ('world', 'Hot')

>>> print format % values

Hello, world. Hot enough for ya?

 

       注意,除了数组和字典外,其他的序列都解释为单个值。

       真正的百分号要用%%表示。

       其他类型:

       >>> format = "Pi with three decimals: %.3f"

>>> 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.'

 

§3.3  字符串格式化的长格式

长版本的字符串格式化:

>>> '%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: 2a'

>>> 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' % 42L

'Using str: 42'

>>> 'Using repr: %r' % 42L

'Using repr: 42L'

       具体介绍参见教材84

      

>>> '%10.2f' % pi # Field width 10, precision 2

' 3.14'

>>> '%.2f' % pi # Precision 2

'3.14'

>>> '%.5s' % 'Guido van Rossum'

'Guido'

 

>>> '%.*s' % (5, 'Guido van Rossum')

'Guido'

 

>>> '%010.2f' % pi

'0000003.14'

注意010在这里不是代表8进制。

 

>>> '%-10.2f' % pi

'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%*.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

 

 

§3.4  字符串方法

       字符串常量:

? 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()

'trondheim hammer dance'

相关的有: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:暂不涉及

      

阅读(1343) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~