6. Python脚本学习笔记六字符串

6. Python脚本学习笔记六字符串

 

                  本篇名言:“心存梦想,机遇就会笼罩着你;心存希望,幸福就会降临于你;心存坚持,快乐就会常伴你;心存真诚,平安就会跟随你;美丽的生活由心而定!”

                  来看下Python下的字符串。

1.  Python字符串格式化

使用%格式化字符串,如下示例。

>>> format="Hello. %s. %senough for ya?"

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

>>> print format % values

Hello. world. Hot enough for ya?

此外String模块提供另一种格式化值的方法:模板字符串。

如下图示例:

>>> from string import Template

>>> s=Template('$x,glorious $x!')

>>> s.substitute(x='slurm')

'slurm,glorious slurm!'

如果本身来$符号,就需要输入连续的两个$$来表示。

 

格式化操作符的右操作可以是任何东西,如果是元组或者映射类型,那么字符串格式化会有所不同。

1.1      简单转换例如:

>>> 'Price of eggs:$%d' % 23

'Price of eggs:$23'

 

>>> 'Hexadecimal price of eggs: %x' % 23

'Hexadecimal price of eggs: 17 '

 

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

 

1.2      字段宽度和精度

转换说明符号,可以包括字段宽度和精度。

                  宽度是转换后的值所保留的最小字符个数,精度是结果中应该包含的最小位数。

>>> from math import pi

>>> '%10f' % pi

' 3.141593'

>>> '%10.2f' % pi

'     3.14'

>>> '%.2f' % pi

'3.14'

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

'Guido'

可以使用*作为字段宽度或者精度(或者两者都使用*),此时数值会从元组参数中读出:

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

'Guido'

 

1.3      符号、对齐和0填充

 

字段宽度和精度之前还可以放置一个“标表”,该标表可以是零,加号,减号或空格。

>>> '%010.2f' % pi

'0000003.14'

这里的0开头不是表示八进制数。

左对齐数,如下,使用-符号来进行左对齐

>>> '%-10.2f' % pi

'3.14     '

在整数前加空格

>>> '% 5d' % 10

'  10'

>>> '% 5d' % -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

 

输入一个宽度,格式化水果价格的表格

主要是使用-实现左对齐,使用’%.2f’来实现保留2个小数。

输出:

Pleaseenter width: 35

===================================

Item                          Price

-----------------------------------

Apples                         0.40

Pears                          0.50

Cantaloupes                    1.92

DriedApricots (16 oz.)        8.00

Prunes(4 lbs.)               12.00

===================================

2.  字符串方法

字符串从String模块中继承了很多方法,有很多的方法,列举特别有用的一些方法。

2.1      Find

查找子字符串。

>>> 'With a moo-moo here. and amoo-moo there'.find('moo')

7

返回字串所在的最左边的索引。

2.2      Join

是split方法的逆方法。

需要添加的队列元素都必须是字符串。

>>> seq=['1','2','3','4','5']

>>> sep='+'

>>> sep.join(seq)

'1+2+3+4+5'

可以用于路径如下图:

>>> dirs='','usr','bin','env'

>>> '/'.join(dirs)

'/usr/bin/env'

2.3      Lower

返回字符串的小写字母版

>>>'ABCDE'.lower()

'abcde'

此外还有title方法,可以变成主题。

 

 

 

 

 

2.4      Replace

替换某字符串的所有匹配项。

>>> 'This is atest'.replace('is','eez')

'Theez eez a test'

 

 

2.5      Split

是join的逆方法。用来将字符串分隔成序列。

>>>'1+2+3+4+5'.split('+')

['1','2', '3', '4', '5']

 

2.6      Strip

去除两侧(不包括内部)的空格的字符串

>>> '            internal withspace is kept     '.strip()

'internal withspace is kept'

可以加入参数,指定要去除的字符

 

2.7      Translate

 

类似replace方法,不过只处理单个字符。可以配合string模块里面的maketrans函数。

使用maketrans来创建用于转换的表,如下实现将英语转换成德语。

就是cs 装换成kz。

>>> from string import maketrans

>>> table=maketrans('cs','kz')

>>> table[97:123]

'abkdefghijklmnopqrztuvwxyz'

>>> 'this is an incredibletest'.translate(table)

'thiz iz an inkredible tezt'

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值