o(* ̄︶ ̄*)o

  博客园  :: 首页  ::  :: 联系 :: 订阅 订阅  :: 管理

python字符串

#
#字符串
#

#3-1 字符串操作
# 索引
# 分片
# 乘法
# 判断成员资格
# 求长度
# 最大/小值

# *** 不允许对字符串分片赋值 ****#

webAddr = "http://www.baidu.com"
print webAddr
#webAddr[-3:] = '.org'         #错误做法

# *******************************#


#3-2 字符串格式化
#ep 
# % 是 转换说明符, 如果 字符串中需要两个 % 则 【%%】就会输出
format = "Hello, %s , Can %s help you?"
values = ("Lily","I")
print format % values                       # 元组中两个元素通过格式化
                                            # 加载到字符串中
											
import math
print u'圆周率 :%.3f' % math.pi             # 点 左边表示 显示占几位数, 右边表示 保留小数点几位         
print u'圆周率 :%10.3f' % math.pi
print u'圆周率 :%-10.3f' % math.pi          # 符号表示左对齐,默认右对齐 
print u'圆周率 :%-10.3f' % math.pi

print ('%+5d' % 10 + '\n' + '%-5d' % -10)
#以下填充相同
print u'圆周率 :% 10.3f' % math.pi          # 空格填充
print u'圆周率 :%010.3f' % math.pi 			# 不是表示八进制,0填充
print u'圆周率 :%' '10.3f' % math.pi        # 空格填充

print "%d, %o, %x, %c, %r, %s"	%(100, 100, 100, 'a', repr(100), str('hehe'))		#常见格式化输出

#【特色输出方式】; 可选择精确度
print '%.*s' % (5, 'Lo ta! wei le bu luo ')	   #精确输出5位							
print '%*.*s' % (10, 3,'Lo ta! wei le bu luo ')	
print '%*s' % (30, 'Lo ta! wei le bu luo ')	



#字符串模板
import string
self_str = string.Template('$x, glorious $x!')     # 声明模板,$+变量名
print self_str.substitute(x = 'hello')             # 形参 直接替换 $x

self_str = string.Template('$x, glorious,$$, $x!') # 如果需要$符号, 则 $$这样写,不会冲突
print self_str.substitute(x = 'hello') 

self_str = string.Template('$x, ${x}glorious,$$ $x!') #如果替换单词的一个部分,需要括号
print self_str.substitute(x = 'hello')  

self_str = string.Template('$x1, ${x3}glorious,$$ $x2!') #字典进行替换
self_list = {}
self_list['x1'] = 'Hello'
self_list['x2'] = 'go'
self_list['x3'] = 'to' 
print self_str.substitute(self_list) 

#ep

width = 70
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', price_width, 8)
print format % (item_width, 'prunes', price_width, 12)
print '=' * width


#3-3 字符串方法
# @func:  find函数 
# @return: 找到的第一个索引,否则 -10
# 跟运算符 in 相同
title = 'we are good friends'
print title.find('are') 
print title.find('out')
print 'are' in title

print title.find('are', 3) 				# 指定 起始位置 查找
print title.find('are', 4, 10) 			# 指定 起始位置 和 结束位置


# @func: join 函数
# 将 多个字符串 进行拼接
# 与 split相反
seq = ['2', '4', '6', '8', '10']        #使用join 元素必须是字符串 或 字符
print '////'.join(seq)     


# @func_name: lower 
# @func :  将字符串进行 转换为小写字母
test = 'Hello, How Are, you?'
print test.lower()


# @func_name: replace
# @func: 替换字符串
print test.replace('o', '++')            # 找不到可替换,则返回原来的样子

# @func_name: split
# @func: 删除 相同的字符 ,截断成列表
print '1+2+3+4+ 5'.split('+')

# @func_name: strip
# @func:  删除字符串头尾的空格
# 也可以指定删除 头尾字符
print '    123333      '.strip()
print '#####123123##123123!!###'.strip('#!') # 不是看做整体删除的,看做单个字符删除

#@func_name: translate
# @func:处理单个字符,可进行多个替换
table = string.maketrans('cs', 'kz') 
print len(table), table
print table[97:123]
print string.maketrans('', '')[97:123]

text = 'this is an incredible test'
print text.translate(table, ' ')  # 用table 进行替换,并删除空格

  

posted on 2015-03-06 16:51  熊本熊の熊  阅读(100)  评论(0)    收藏  举报