Python 使用字符串

1、字符串是不可变的,不能进行分片赋值

2、格式化字符串的%s部分称为转换说明符(conversion specifier),它们标记了需要插入转换值的位置

format = "Hello %s,%s enough for ya?"
# 只有元组和字典可以格式化一个以上的值
values = ('world','Hot')
print format % values
# Hello world,Hot enough for ya?

f1 = "pi with three decimals:%.3f"
from math import pi
print f1 % pi
# pi with three decimals:3.142
View Code

3、模板字符串(from string import Template),使用函数substitute将关键字foo替换字符串中的$foo.方法safe_substitute不会因缺少值或者不正确使用$字符而出错

from string import Template
s = Template('${x},glorious ${x}')
print s.substitute(x='Jimmy')
# Jimmy,glorious Jimmy

s1 = Template("Make $$ selling $y")
print s1.substitute(y='203')
# Make $ selling 203

s2 = Template('A $thing must never $action')
d = {}
d['thing'] = 'gentleman'
d['action'] = 'show his socks'
print s2.substitute(d)
print s2.safe_substitute()
# A gentleman must never show his socks
# A $thing must never $action
模板字符串

4、转换类型

  d,i  带符号的十进制整数

  o    不带符号的八进制

  u    不带符号的十进制

  x    不带符号的十六进制(小写)

  X    不带符号的十六进制(大写)

  e    科学记数法表示的浮点数(小写)

  E    科学记数法表示的浮点数(大写)

  f,F  十进制浮点数

  C    单字符(接受整数或者单字符单字符串)

  r    字符串(使用repr转换任意Python对象)

  s    字符串(使用str转换任意Python对象)

## 简单转换
print 'Price of eggs:$%d' % 42
# Price of eggs:$42
print 'Hexadecimal price of eggs:%x' % 42
# Hexadecimal price of eggs:2a

from math import pi
print 'PI:%f' % pi
# PI:3.141593

print 'Using str %s' % 42L
# Using str 42
print 'Using repr:%r' % 42L
# Using repr:42L

## 字符宽度和精度(参数均为整数)(通过点号分隔)
print  "\'%10f\'" % pi # 字段宽 10
# '  3.141593'
print  "\'%10.2f\'" % pi # 字段宽 10 , 精度 2
# '      3.14'
print  "\'%.2f\'" % pi # 精度 2
# '3.14'
print '%.5s' % 'Guido van Rossum'
# Guido

# 可以使用星号(*)作为字段宽度或者精度(或者两者都使用*)
print '%.*s' % (5,'Guido Van Rossum')

## 符号、对齐和0填充(在字段宽度和精度值之前还可以放置一个'标表',该标表可以是零、加号、减号或空格)
print  "\'%010.2f\'" % pi
# '0000003.14'
# 减号(-)用来左对齐数值
print  "\'%-10.2f\'" % pi
# '3.14      '
# 加号(+) 表示不管正数还是负数都标出符号
print ('%+5d' % 10) + '\n' + ('%+5d' % -10)
# +10
# -10
View Code

 5、字符串方法

  find 方法可以在一个较长的字符串中查找子字符串,返回子串所在位置的最左端索引,如果没有找到则返回-1

  join 方法是非常重要的字符串方法,它是split方法的逆方法,用来在队列中添加元素

  lower 方法返回字符串的小写字母版

  title 方法会将字符串所有单词的首字母大写,而其他小写

  replace 方法返回某字符串的索引匹配项均被替换之后得到字符串

  split 方法用来字符串分割成序列

  strip 方法返回去除两侧(不包含内部)指定字符的字符串

  lstrip  方法返回去除左边(不包含内部)指定字符的字符串

  translate 方法替换字符串,只处理单个字符,可以同时进行多个替换

## find
print 'With a moo-moo here,and a moo-moo there'.find('moo')
# 7
title = "Monty Python's Flying Circus"
print title.find('Monty')
# 0
print title.find('Python')
# 6
print title.find('Flying')
# 15
print title.find('aaa')
# -1

subject = '$$$ Get rich now!!! $$$'
print subject.find('$$$')
# 0
print subject.find('$$$',1) # 只提供起始点
# 20
print subject.find('!!!')
# 16
print subject.find('!!!',0,16) # 提供起始点和结束点
# -1

## join
seq = ['1','2','3','4','5']
sep = '+'
print sep.join(seq)
# 1+2+3+4+5
dirs = '','user','bin','env'
print '/'.join(dirs)
# /user/bin/env
print 'C:' + '\\'.join(dirs)
# C:\user\bin\env

## lower
print 'AbcDEFgh'.lower()
# abcdefgh
if 'Gumby' in ['gumby','smith','jones']:print 'Found it'
# nothing
if 'Gumby'.lower() in ['gumby','smith','jones']:print 'Found it'
# Found it

## replace
print 'This is a test'.replace('is','eez')
# Theez eez a test
print '1+2+3+4+5+6'.split('+')
# ['1', '2', '3', '4', '5', '6']
print 'Using the default'.split()
# ['Using', 'the', 'default']

## strip
print '  who am I  '.strip()
# who am I
print '*** SPAM * for * everyone!!! ***'.strip(' *!')
# SPAM * for * everyone
print '*** SPAM * for * everyone!!! ***'.lstrip(' *!')

## translate
from string import maketrans

intab = 'aeiou'
outtab = '12345'

trantab = maketrans(intab,outtab)

str = 'this is string example'
print str.translate(trantab)
# th3s 3s str3ng 2x1mpl2
字符串方法

 

posted on 2017-05-27 15:49  7890陈  阅读(259)  评论(0编辑  收藏  举报

导航