Python 3 的源码的默认编码方式为 UTF-8

在Python 3,所有的字符串都是使用Unicode编码的字符序列。

utf-8 是一种将字符编码成字节序列的方式。字节即字节,并非字符。字符在计算机内只是一种抽象。字符串则是一种抽象的序列。在这里我们只讨论字符串,不讨论字节。


在Python 中,字符串可以用单引号括起来,也可以用双引号,甚至是三引号。

但如果字符串中含有单引号,你应该用双引号来括,或者用转义符加单引号括起来。含有双引号的同理。

三引号的字符串可以换行!

 

[python] view plain copy print?

  1. >>> 'Let's go!'  

  2. SyntaxError: invalid syntax  

  3. >>> "Let's go!"  

  4. "Let's go!"  

  5. >>> 'Let\'s go!'  

  6. "Let's go!"  

  7. >>> '''''begin 

  8. and 

  9. next 

  10. end'''  

  11. 'begin\nand\nnext\nend'  

>>> 'Let's go!'
SyntaxError: invalid syntax
>>> "Let's go!"
"Let's go!"
>>> 'Let\'s go!'
"Let's go!"
>>> '''begin
and
next
end'''
'begin\nand\nnext\nend'


字符串是不可修改的,这点很重要!你可以把它想象为字符组成的元组。

 

 

[python] view plain copy print?

  1. >>> s = "HelloWorld"  

  2. >>> s[0] = 'h'  

  3. Traceback (most recent call last):  

  4.   File "<pyshell#123>", line 1in <module>  

  5.     s[0] = 'h'  

  6. TypeError: 'str' object does not support item assignment  

>>> s = "HelloWorld"
>>> s[0] = 'h'
Traceback (most recent call last):
  File "<pyshell#123>", line 1, in <module>
    s[0] = 'h'
TypeError: 'str' object does not support item assignment


如果你想修改它,可以先转换成列表,修改完成后再转为字符串。

 

 

[python] view plain copy print?

  1. >>> s  

  2. 'HelloWorld'  

  3. >>> L = list(s)  

  4. >>> L  

  5. ['H''e''l''l''o''W''o''r''l''d']  

  6. >>> L[0] = 'h'  

  7. >>> ''.join(L) #这是什么?别着急,下面我们会谈到   

  8. 'helloWorld'  

>>> s
'HelloWorld'
>>> L = list(s)
>>> L
['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
>>> L[0] = 'h'
>>> ''.join(L) #这是什么?别着急,下面我们会谈到
'helloWorld'


字符串可以进行直接拼接,但如果是两个变量代表的字符串,你还是用 + 号吧

 

 

[python] view plain copy print?

  1. >>> s = "Hello""World"  

  2. >>> s  

  3. 'HelloWorld'  

  4. >>> s1 = "Hello"  

  5. >>> s2 = "World"  

  6. >>> s1s2  

  7. Traceback (most recent call last):  

  8.   File "<pyshell#138>", line 1in <module>  

  9.     s1s2  

  10. NameError: name 's1s2' is not defined  

  11. >>> s1+s2  

  12. 'HelloWorld'  

>>> s = "Hello""World"
>>> s
'HelloWorld'
>>> s1 = "Hello"
>>> s2 = "World"
>>> s1s2
Traceback (most recent call last):
  File "<pyshell#138>", line 1, in <module>
    s1s2
NameError: name 's1s2' is not defined
>>> s1+s2
'HelloWorld'

 


字符串操作和方法:

len(s)  返回字符串长度

x in s 查询 x 是否在 s 中

 

[python] view plain copy print?

  1. >>> s = "HelloWorld"  

  2. >>> len(s)  

  3. 10  

  4. >>> "ll" in s  

  5. True  

>>> s = "HelloWorld"
>>> len(s)
10
>>> "ll" in s
True


 

s.find( x )  在字符串 s 中找子串 x ,找到则返回最左端的索引,找不到则返回-1

 

[python] view plain copy print?

  1. >>> s  

  2. 'HelloWorld'  

  3. >>> s.find("l")  

  4. 2  

  5. >>> s.find('w')  

  6. -1  

>>> s
'HelloWorld'
>>> s.find("l")
2
>>> s.find('w')
-1


 

s.splitlines() 将多行字符串分割成多个单行字符串组成的列表,换行符被吸收

 

[python] view plain copy print?

  1. >>> s = '''''begin 

  2. ...then.. 

  3. ...next.. 

  4. end...'''  

  5. >>> s.splitlines()  

  6. ['begin''...then..''...next..''end...']  

>>> s = '''begin
...then..
...next..
end...'''
>>> s.splitlines()
['begin', '...then..', '...next..', 'end...']


 

s.split( x ) 以 x 作为分隔符将 s 分割成一个字符串列表,如果不提供x,则程序会自动将所有空格和换行作为分隔符分割

 

[python] view plain copy print?

  1. >>> s = "here#there"  

  2. >>> s.split('#') # #作为分隔符  

  3. ['here''there']  

  4. >>> s = '''''begin 

  5. .then.. and 

  6. .next. 

  7. end'''  

  8. >>> s.split()  #默认情况将所有换行和空格都分割   

  9. ['begin''.then..''and''.next.''end']  

>>> s = "here#there"
>>> s.split('#') # #作为分隔符
['here', 'there']
>>> s = '''begin
.then.. and
.next.
end'''
>>> s.split()  #默认情况将所有换行和空格都分割
['begin', '.then..', 'and', '.next.', 'end']


 

s.lower()   返回s 的小写形式的字符串

s.upper() 返回 s 的大写形式的字符串

 

[python] view plain copy print?

  1. >>> s = 'HelloWorld'  

  2. >>> s.lower()  

  3. 'helloworld'  

  4. >>> s.upper()  

  5. 'HELLOWORLD'  

>>> s = 'HelloWorld'
>>> s.lower()
'helloworld'
>>> s.upper()
'HELLOWORLD'


 

s.join( seq )  split 的逆方法,将序列 seq 用 s 连接起来,必须是字符串序列

 

[python] view plain copy print?

  1. >>> L = ['1','33','42']  

  2. >>> '+'.join(L)  #用+来连接   

  3. '1+33+42'  

  4. >>> L = list(s) #拆开s   

  5. >>> L  

  6. ['H''e''l''l''o''W''o''r''l''d']  

  7. >>> ''.join(L)  #粘合了。。。   

  8. 'HelloWorld'  

>>> L = ['1','33','42']
>>> '+'.join(L)  #用+来连接
'1+33+42'
>>> L = list(s) #拆开s
>>> L
['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
>>> ''.join(L)  #粘合了。。。
'HelloWorld'


 

s.replace( x, y)  将 s 中所有匹配 x 的项用 y 代替,如果找不到,那就直接返回 s 咯

 

[python] view plain copy print?

  1. >>> s  

  2. 'HelloWorld'  

  3. >>> s.replace("World","Cheng")  

  4. 'HelloCheng'  

  5. >>> s.replace("world","Cheng"#知道为什么吧...   

  6. 'HelloWorld'  

>>> s
'HelloWorld'
>>> s.replace("World","Cheng")
'HelloCheng'
>>> s.replace("world","Cheng") #知道为什么吧...
'HelloWorld'


 

s.strip( x ) 如果不提供 x,那么返回去除了首尾两侧空格的字符串,如果提供了字符串 x,那么将去除s首尾所有在 x 中的字符并返回

 

[python] view plain copy print?

  1. >>> s = "   Hi,I'm Alice!   "  

  2. >>> s.strip()  

  3. "Hi,I'm Alice!"  

  4. >>> s.strip("! ")  #这是两个字符哦   

  5. "Hi,I'm Alice" #少了一个感叹号哦!  

>>> s = "   Hi,I'm Alice!   "
>>> s.strip()
"Hi,I'm Alice!"
>>> s.strip("! ")  #这是两个字符哦
"Hi,I'm Alice" #少了一个感叹号哦!


 

再次注意:以上方法都没有改变原字符串,字符串是不可改变的!


以下简单看看:

s.starstwith( x )  测试 s 是否以 x 开头 

s.endswith( x )  测试 s 是否以 x 结尾 

s.isalnum()  测试 s 是否全是字母和数字,并至少有一个字符

s.isalpha()   测试 s 是否全是字母,并至少有一个字符 

s.isdigit()  测试 s 是否全是数字,并至少有一个字符 

s.isspace()  测试 s 是否全是空白字符,并至少有一个字符 

s.islower() 测试 s 中的字母是否全是小写 

s.isupper() 测试 s 中的字母是否便是大写

s.istitle() 测试 s 是否是首字母大写的


让我们重点关注一个强大的格式化方法 format ,看下面代码

 

[python] view plain copy print?

  1. >>> name = 'Jonh'  

  2. >>> call = '13560300xxx'  

  3. >>> "{0}'s telephone number is {1}".format(name,call) # (1)   

  4. "Jonh's telephone number is 13560300xxx"  

  5. >>> addr = "A103"  

  6. >>> "{0}'s telephone number is {1} and his address is {2}".format(name,call,addr) #(2)   

  7. "Jonh's telephone number is 13560300xxx and his address is A103"  

>>> name = 'Jonh'
>>> call = '13560300xxx'
>>> "{0}'s telephone number is {1}".format(name,call) # (1)
"Jonh's telephone number is 13560300xxx"
>>> addr = "A103"
>>> "{0}'s telephone number is {1} and his address is {2}".format(name,call,addr) #(2)
"Jonh's telephone number is 13560300xxx and his address is A103"


(1)句中,字符串中 {0} 被 format 的第一个参数代替,{1} 被第二个参数代替。两个参数不够?事实上,你可以给予它任意多个参数,然后用相同个数的替换字段进行替换。什么是替换字段?{0},{1}就叫做替换字段。我们在第(2)句中使用了3个替换字段,{0}对应name, {1}对应call,{2}对应addr。再多的参数也类似于刚才的做法。

 

那么,仅仅是这样?当然不是!让我们继续看

 

[python] view plain copy print?

  1. >>> L = [2,3,5,7]  

  2. >>> print("x is {0[0]}, y is {0[2]}".format(L))  

  3. is 2, y is 5  

>>> L = [2,3,5,7]
>>> print("x is {0[0]}, y is {0[2]}".format(L))
x is 2, y is 5

{0[0]} 表示L[0],{0[2]} 表示L[2],它们叫做复合字段名,你可以:

 

(1)使用列表作为参数,并且通过下标索引来访问其元素(跟上一例类似)

(2)使用字典作为参数,并且通过键来访问其值

 

[python] view plain copy print?

  1. >>> d  

  2. {'b'2'a'1}  

  3. >>> print("x is {0[a]}, y is {0[b]}".format(d))  

  4. is 1, y is 2  

  5. >>> d = {2:3.5,7:4.5}  

  6. >>> print("x is {0[2]}, y is {0[7]}".format(d))  

  7. is 3.5, y is 4.5  

>>> d
{'b': 2, 'a': 1}
>>> print("x is {0[a]}, y is {0[b]}".format(d))
x is 1, y is 2
>>> d = {2:3.5,7:4.5}
>>> print("x is {0[2]}, y is {0[7]}".format(d))
x is 3.5, y is 4.5

d 为字典,a 、b为键,{0[a]} 对应到了值2(注意:是a,b,不是'a', 'b')

 

(3)使用模块作为参数,并且通过名字来访问其变量及函数

 

[python] view plain copy print?

  1. >>> print("{0.random}".format(random))  

  2. <built-in method random of Random object at 0x022D61F8>  

>>> print("{0.random}".format(random))
<built-in method random of Random object at 0x022D61F8>

 

(4)使用类的实例作为参数,并且通过名字来访问其方法和属性

 

[python] view plain copy print?

  1. >>> class A:  

  2.     pass  

  3.   

  4. >>> x = A()  

  5. >>> print("The class is {0.__class__}".format(x))  

  6. The class is <class '__main__.A'>  

>>> class A:
	pass

>>> x = A()
>>> print("The class is {0.__class__}".format(x))
The class is <class '__main__.A'>

 

(5)以上方法的任意组合


替换字段除了整数,你还可以使用参数名字

 

[python] view plain copy print?

  1. >>> print("{name}'s telephone number is {call}".format(name = "Jonh",call = 69993))  

  2. Jonh's telephone number is 69993  

>>> print("{name}'s telephone number is {call}".format(name = "Jonh",call = 69993))
Jonh's telephone number is 69993


在替换域中,你还可以使用格式说明符。冒号 : 标示格式说明符的开始。

 

格式说明符请见:http://blog.csdn.net/jcjc918/article/details/9354815

[python] view plain copy print?

  1. >>> pi = 3.141592653  

  2. >>> print("The pi is {0:10.3f}".format(pi)) # 0:10.3f 表示输出宽度为10,保留三位小数,浮点数   

  3. The pi is      3.142  

>>> pi = 3.141592653
>>> print("The pi is {0:10.3f}".format(pi)) # 0:10.3f 表示输出宽度为10,保留三位小数,浮点数
The pi is      3.142