python之字符串

字符串immutable, 所以不能只改变一个字符串的一个字符或者子串,但可以通过拼凑一个旧串的各个部分来得到一个新串。

1 字符串操作符

标准类型操作符和标准序列操作符略过
字符串操作符
格式化操作符( % )

    符   号描述
      %c  格式化字符及其ASCII码
      %s  格式化字符串
      %d  格式化整数
      %u  格式化无符号整型
      %o  格式化无符号八进制数
      %x  格式化无符号十六进制数
      %X  格式化无符号十六进制数(大写)
      %f  格式化浮点数字,可指定小数点后的精度
      %e  用科学计数法格式化浮点数
      %E  作用同%e,用科学计数法格式化浮点数
      %g  根据值的大小决定使用%f活%e
      %G  作用同%g,根据值的大小决定使用%f活%e
      %p  用十六进制数格式化变量的地址

 

 

 

 

 

 

 

 

 

 

 

 

 


原生字符串( r )
Unicode字符串( u )

转义字符

转义字符描述
\(在行尾时) 续行符
\\ 反斜杠符号
\' 单引号
\" 双引号
\a 响铃
\b 退格(Backspace)
\e 转义
\000
\n 换行
\v 纵向制表符
\t 横向制表符
\r 回车
\f 换页
\oyy 八进制数yy代表的字符,例如:\o12代表换行
\xyy 十进制数yy代表的字符,例如:\x0a代表换行
\other 其它的字符以普通格式输出

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. 字符串内建函数

标准类型内建函数略过
序列类型内建函数示例几个

>>> max('abc'),
'c'
>>> min('abc')
'a'
>>> for i, j in enumerate('hello'):
print i, j
>>> zip('hello', 'world')
[('h', 'w'), ('e', 'o'), ('l', 'r'), ('l', 'l'), ('o', 'd')]

字符串类型函数

1.字符串连接

①:+, +=
②:''.join()

>>> lis = ['apple','banana','china']
>>> '-'.join(lis) 
apple-banana-china 
>>> ''.join(lis) 
applebananachina
>>> '-'.join('hello') 
h-e-l-l-o

 

2.字符串截取

①string[x:y]
②:''.split()

>>> string = 'h-e-l-l-o'
>>> string.split('-') 
['h', 'e', 'l', 'l', 'o'] 
>>> string.split('-',2) 
['h', 'e', 'l-l-o']

③strip()

#s为字符串,rm为要删除的字符序列
#s.strip(rm)        删除s字符串中开头、结尾处,位于 rm删除序列的字符
#s.lstrip(rm)       删除s字符串中开头处,位于 rm删除序列的字符
#s.rstrip(rm)      删除s字符串中结尾处,位于 rm删除序列的字符
ps: 当rm为空时,默认删除空白符(包括'\n', '\r',  '\t',  ' ')
>>> a = '    123'
>>> a.strip()
'123'
>>> a = '123abc'
>>> a.strip('12')
'3abc'
>>> a.strip('21')
'3abc'

 

3.字符串的查找与替换

① find(), rfind()
字符串中找到substring则返回索引(如果字符串中有多个结果则返回第一次出现的索引),没找到返回-1

>>> string = 'I am Fishhat'
>>> string.find('F') 
5
>>> find('f') 
-1 
>>> print string.find('h',5,-1) #返回找到的第一个的索引
8 
>>> print string.rfind('h') #rfind()从尾部开始找
9

②替换 replace()

>>> string = 'AAAAABBBBBDDDDD'
>>> string.replace('D','C') #把字符串中所有的D替换为C
'AAAAABBBBBCCCCC'
>>> string.replace('A','a',3) #替换字符串中的3个A为a
'aaaAABBBBCCCCC' #replace()函数的操作不会影响原字符串.只是拷贝原字符串然后进行操作而已

③startswith() 函数和 endswith() 函数

>>> string = 'fishhat'
>>> string.startswith('fi')
True
>>> string.startswith('sh',2,4)
True
>>> string.endswith('hat')
True
>>> string.endswith('ha',4,6)
True
④string.count(sub, start= 0,end=len(string))
string = 'hello'

 

4.字符串对齐:

①:string.center(int[,str])

>>> string = 'Fishhat'
>>> string.center(55)
' Fishhat '
>>> string.center(55,'*')
'************************Fishhat************************'

②:string.ljust(int[,str])

>>> string.ljust(55)
'Fishhat '
>>> string.string.ljust(55,'*')
'Fishhat************************************************'

③:string.rjust(int[,str])

>>> string.ljust(55)
' Fishhat'
>>> string.ljust(55,'*')
'************************************************Fishhat'

④:%(int)s

>>> '% 55s' % string
' Fishhat'
>>> '% -55s' % string

 

5.字符串其他处理:

string.capitalize() #把字符串的第一个字符大写
string.upper() #转换 string 中的小写字母为大写
string.lower() #转换 string 中所有大写字符为小写.

 

 

 

3. Unicode字符串

http://ez2learn.com/basic/unicode.html

# coding: utf8


s1 = ''
print repr(s1)
print len(s1)
u1 = u''
print repr(u1)
print len(u1)
s2 = u1.encode('utf8')
print repr(s2)
u2 = s2.decode('utf8')
print repr(u2)


# 对unicode进行解码时错误的
s3 = u1.decode('utf8')
# 对str进行编码也是错误的
u3 = s1.encode('utf8')

 

 

  

 2015-05-24

posted @ 2015-05-24 23:20  whu.yt  阅读(212)  评论(0编辑  收藏  举报