Python 中的字符串处理函数

来源:互联网 发布:php工作经验 编辑:程序博客网 时间:2024/06/08 15:19
一、python中字符串的声明
转载自:http://blog.csdn.net/dolphin_h/article/details/10305959
1.使用单引号(')
你可以用单引号指示字符串,就如同'Quote me on this'这样。所有的空白,即空格和制表符都照原样保留
2.使用双引号(")
在双引号中的字符串与单引号中的字符串的使用完全相同,例如"What's your name?"。当字符串中出现单引号时,声明就是用双引号。
3.使用三引号('''或""")
利用三引号,你可以指示一个多行的字符串。你可以在三引号中自由的使用单引号和双引号。例如:
'''This is a multi-line string. This is the first line.
This is the second line.
"What's your name?," I asked.
He said "Bond, James Bond."
'''
4.转义符
假设你想要在一个字符串中包含一个单引号('),那么你该怎么指示这个字符串?例如,这个字符串是What's your name?。你肯定不会用'What's your name?'来指示它,因为Python会弄不明白这个字符串从何处开始,何处结束。所以,你需要指明单引号而不是字符串的结尾。可以通过 转义符 来完成这个任务。你用\'来指示单引号——注意这个反斜杠。现在你可以把字符串表示为'What\'s your name?'。
另一个表示这个特别的字符串的方法是"What's your name?",即用双引号。类似地,要在双引号字符串中使用双引号本身的时候,也可以借助于转义符。另外,你可以用转义符\\来指示反斜杠本身。
值得注意的一件事是,在一个字符串中,行末的单独一个反斜杠表示字符串在下一行继续,而不是开始一个新的行。例如:
"This is the first sentence.\
This is the second sentence."
等价于"This is the first sentence. This is the second sentence."
换行符为\n
5.自然字符串
如果你想要指示某些不需要如转义符那样的特别处理的字符串,那么你需要指定一个自然字符串。自然字符串通过给字符串加上前缀r或R来指定。例如r"Newlines are indicated by \n"。
6.Unicode字符串
Unicode是书写国际文本的标准方法。如果你想要用你的母语如北印度语或阿拉伯语写文本,那么你需要有一个支持Unicode的编辑器。类似地,Python允许你处理Unicode文本——你只需要在字符串前加上前缀u或U。例如,u"This is a Unicode string."。
记住,在你处理文本文件的时候使用Unicode字符串,特别是当你知道这个文件含有用非英语的语言写的文本。
7.字符串是不可变的
这意味着一旦你创造了一个字符串,你就不能再改变它了。虽然这看起来像是一件坏事,但实际上它不是。我们将会在后面的程序中看到为什么我们说它不是一个缺点。
按字面意义级连字符串
如果你把两个字符串按字面意义相邻放着,他们会被Python自动级连。例如,'What\'s' 'your name?'会被自动转为"What's your name?"。
**给C/C++程序员的注释
在Python中没有专门的char数据类型。确实没有需要有这个类型,我相信你不会为此而烦恼。
**给Perl/PHP程序员的注释
记住,单引号和双引号字符串是完全相同的——它们没有在任何方面有不同。
**给正则表达式用户的注释
一定要用自然字符串处理正则表达式。否则会需要使用很多的反斜杠。例如,后向引用符可以写成'\\1'或r'\1'。
二、Python下的字符串函数【节选】
转载自:http://blog.csdn.net/dolphin_h/article/details/10305959
String模块中的常量:
string.digits:数字0~9
string.letters:所有字母(大小写)
string.lowercase:所有小写字母
string.printable:可打印字符的字符串
string.punctuation:所有标点
string.uppercase:所有大写字母
>>> import string  
>>> string.digits  
'0123456789'  
>>> string.letters  
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'  
>>> string.lowercase  
'abcdefghijklmnopqrstuvwxyz'  
>>> string.printable  
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'  
>>> string.punctuation  
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'  
>>> string.uppercase  
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'  
三、常用的Python 内置的各种字符串处理函数的使用方法
转载自:http://blog.csdn.net/jinxingfeng_cn/article/details/8575934
str='python String function'

    生成字符串变量str='python String function'
     
    字符串长度获取:len(str)
    例:print '%s length=%d' % (str,len(str))
     
    字母处理
    全部大写:str.upper()
    全部小写:str.lower()
    大小写互换:str.swapcase()
    首字母大写,其余小写:str.capitalize()
    首字母大写:str.title()
    print '%s lower=%s' % (str,str.lower())
    print '%s upper=%s' % (str,str.upper())
    print '%s swapcase=%s' % (str,str.swapcase())
    print '%s capitalize=%s' % (str,str.capitalize())
    print '%s title=%s' % (str,str.title())

    格式化相关
    获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
    获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
    获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
    获取固定长度,右对齐,左边不足用0补齐
    print '%s ljust=%s' % (str,str.ljust(20))
    print '%s rjust=%s' % (str,str.rjust(20))
    print '%s center=%s' % (str,str.center(20))
    print '%s zfill=%s' % (str,str.zfill(20))

    字符串搜索相关
    搜索指定字符串,没有返回-1:str.find('t')
    指定起始位置搜索:str.find('t',start)
    指定起始及结束位置搜索:str.find('t',start,end)
    从右边开始查找:str.rfind('t')
    搜索到多少个指定字符串:str.count('t')
    上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
    print '%s find nono=%d' % (str,str.find('nono'))
    print '%s find t=%d' % (str,str.find('t'))
    print '%s find t from %d=%d' % (str,1,str.find('t',1))
    print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
    #print '%s index nono ' % (str,str.index('nono',1,2))
    print '%s rfind t=%d' % (str,str.rfind('t'))
    print '%s count t=%d' % (str,str.count('t'))

    字符串替换相关
    替换old为new:str.replace('old','new')
    替换指定次数的old为new:str.replace('old','new',maxReplaceTimes)
    print '%s replace t to *=%s' % (str,str.replace('t', '*'))
    print '%s replace t to *=%s' % (str,str.replace('t', '*',1))

    字符串去空格及去指定字符
    去两边空格:str.strip()
    去左空格:str.lstrip()
    去右空格:str.rstrip()
    去两边字符串:str.strip('d'),相应的也有lstrip,rstrip
    str=' python String function '
    print '%s strip=%s' % (str,str.strip())
    str='python String function'
    print '%s strip=%s' % (str,str.strip('d'))

    按指定字符分割字符串为数组:str.split(' ')
    默认按空格分隔
    str='a b c de'
    print '%s strip=%s' % (str,str.split())
    str='a-b-c-de'
    print '%s strip=%s' % (str,str.split('-'))

    字符串判断相关
    是否以start开头:str.startswith('start')
    是否以end结尾:str.endswith('end')
    是否全为字母或数字:str.isalnum()
    是否全字母:str.isalpha()
    是否全数字:str.isdigit()
    是否全小写:str.islower()
    是否全大写:str.isupper()
    str='python String function'
    print '%s startwith t=%s' % (str,str.startswith('t'))
    print '%s endwith d=%s' % (str,str.endswith('d'))
    print '%s isalnum=%s' % (str,str.isalnum())
    str='pythonStringfunction'
    print '%s isalnum=%s' % (str,str.isalnum())
    print '%s isalpha=%s' % (str,str.isalpha())
    print '%s isupper=%s' % (str,str.isupper())
    print '%s islower=%s' % (str,str.islower())
    print '%s isdigit=%s' % (str,str.isdigit())
    str='3423'
    print '%s isdigit=%s' % (str,str.isdigit())

0 0