1、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'。
2、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'
3、经常使用的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())