```python
>>> fruit="banana"
>>> letter=fruit[1]
>>> print letter
a
>>> len(fruit)
6
```
##7.3 走访以及for循环
```python
index=0
while index < len(fruit):
letter = fruit[index]
print letter
index += 1
```
##7.4 字符串切片
```
>>> s="Peter jackson, all you done is great"
>>> print s[0:5]
Peter
>>> print s[6:13]
jackson
```
如果你省略了第一个索引,切片将会在字符串的起点开始,如果你省略了第二个索引,切片将会以字符串的末端作为结束。
##7.5 字符串比较
比较运算符一样可以运用在字符串上,为了确认两个字符串是否相等。
```python
if word == "banana":
print "Yes, we have no bananas!"
if word < "banana":
print "Your word, " + word + ", comes before banana."
elif word > "banana":
print "Your word, " + word + ", comes after banana"
else:
print "Yes, we have no bananas"
```
##7.7 in运算符
```python
>>> ‘p‘ in ‘person‘
True
>>> ‘a‘ in ‘person‘
False
>>> ‘er‘ in ‘person‘
True
```
##7.10 选择性参数
```python
def find(string, ch, start=0)
```
##7.11 string模块
```python
>>> import string
>>> dir(string)
[‘Formatter‘, ‘Template‘, ‘_TemplateMetaclass‘, ‘__builtins__‘, ‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘_float‘, ‘_idmap‘, ‘_idmapL‘, ‘_int‘, ‘_long‘, ‘_multimap‘, ‘_re‘, ‘ascii_letters‘, ‘ascii_lowercase‘, ‘ascii_uppercase‘, ‘atof‘, ‘atof_error‘, ‘atoi‘, ‘atoi_error‘, ‘atol‘, ‘atol_error‘, ‘capitalize‘, ‘capwords‘, ‘center‘, ‘count‘, ‘digits‘, ‘expandtabs‘, ‘find‘, ‘hexdigits‘, ‘index‘, ‘index_error‘, ‘join‘, ‘joinfields‘, ‘letters‘, ‘ljust‘, ‘lower‘, ‘lowercase‘, ‘lstrip‘, ‘maketrans‘, ‘octdigits‘, ‘printable‘, ‘punctuation‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitfields‘, ‘strip‘, ‘swapcase‘, ‘translate‘, ‘upper‘, ‘uppercase‘, ‘whitespace‘, ‘zfill‘]
>>> type(string.digits)
<type ‘str‘>
>>> type(string.find)
<type ‘function‘>
>>> print string.digits
0123456789
>>> print string.find.__doc__
find(s, sub [,start [,end]]) -> in
Return the lowest index in s where substring sub is found,
such that sub is contained within s[start,end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
>>> string.find("banana","na")
2
>>> string.find("bob","b", 1,2)
-1
```
##7.12 字符的分类
```python
>>> print string.lowercase
abcdefghijklmnopqrstuvwxyz
>>> print string.uppercase
ABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> print string.whitespace
>>>
```
string.whitespace中包含空格、tab(\t)、新行(\n)
##7.13 字符串的格式化
```
>>> "His name is %s." % "Arthur"
‘His name is Arthur.‘
>>> name="lifeng"
>>> age=30
>>> "I am %s and I am %d years old" % (name,age)
‘I am lifeng and I am 30 years old‘
```
***
#9 tuple(元组)
##9.1 可变性与tuple
字符串是不可变型的, 但是列表是可变型的。
python还有一个称谓tuple的型态。它和列表非常相似,只不过它是不可变的。
从语法上说tuple是一系列用逗号分隔的值, 一个元素的tuple必须在最后加上一个逗号,否则会被当成字符串:
```
>>> tuple=‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘
>>> type(tuple)
<type ‘tuple‘>
>>> t1=(‘a‘, ‘b‘)
>>> t2=(‘a‘,)
>>> type(t2)
<type ‘tuple‘>
>>> t3=(‘a‘)
>>> type(t3)
<type ‘str‘>
>>> tuple[0]
‘a‘
>>> tuple[1:3]
(‘b‘, ‘c‘)
```
##9.2 tuple指派
数值交换,传统的写法
```
>>> a=10
>>> b=20
>>> temp=a
>>> a=b
>>> b=temp
>>> a
20
>>> b
10
```
tuple指派的写法
```
>>> a=10
>>> b=20
>>> a,b=b,a
>>> a
20
>>> b
10
```
三值互换:
```
>>> a,b,c=10,20,30
>>> a,b,c=c,a,b
>>> a
30
>>> b
10
>>> c
20
```
##9.4 随机数字
random在数学上说并不是真正随机的。但是可以满足我们日常的要求:
```
>>> import random
>>> for i in range(10):
... x = random.random()
... print x
...
0.684719929612
0.0464362929874
0.395376767428
0.800637456994
0.99066361106
0.765716907162
0.989860584657
0.413398095796
0.935161433805
0.0607366842634
```
***
#10. 字典
***
#11. 文件和异常
当一个程序运作的时候,它的数据会在内存中,当这个程序结束的时候,数据就会丢失,要存储就应该将这些放入一个档案中。
```
>>> f=open("test.dat", "w")
>>> print f
<open file ‘test.dat‘, mode ‘w‘ at 0x7f706b603d20>
>>>
```
写文件
```
>>> f.write("now learning python")
>>> f.write("now stop writing python")
>>> f.close()
```
读文件。 f.read()会读取文件的全部内容
```
>>> f=open("test.dat", "r")
>>> text = f.read()
>>> print text
new line
>>>
```