# Python Core Programming **Repository Path**: hjxown/Python-Core-Programming ## Basic Information - **Project Name**: Python Core Programming - **Description**: The practise of Python Core Programming - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2014-01-23 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README README.org

README.org

Table of Contents

1 第六章:序列:字符串、列表和元组    字符串 列表 元组

1.1 字符串

1.1.1 操作符

in, not in, +, *, 切片操作符:[], [:], [::]
  • 切片操作
>>> string = "hello world!"
>>> string[1]
'e'
>>> string [1:3]
'el'
>>> string[3:1]
''
>>> string[-1:0]
''
>>> string[0:-1]
'hello world'
>>> '步进操作'
'\xe6\xad\xa5\xe8\xbf\x9b\xe6\x93\x8d\xe4\xbd\x9c'
>>> string[::1]
'hello world!'
>>> string[::2]
'hlowrd'
>>> string[::3]
'hlwl'
>>> string[::-1]
'!dlrow olleh'
>>>
  • in, not in操作符
>>> string = 'hello world!'
>>> 'hello' in string
True
>>> 'hello' not in string
False
  • string 模块
>>> import string
>>> string.uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'

>>> 'hello' + 'world!'
'helloworld!'
>>> 'hello' + ' ' + 'world!'
'hello world!'
>>> s = 'hello' + ' ' + 'world!'
>>> string.upper(s[:3])
'HEL'
>>> s
'hello world!'

>>> s = string.Template('My name is ${name} and my age is ${age}')
>>> print s.substitute(name='hujianxin', age=21)
My name is hujianxin and my age is 21

1.1.2 内建函数

类型转换可操作
list()enumerate() (转换为字典序列)
str()len()
unicode()max()
basestring()min()
tuple()reversed()
chr()sorted()
ord()sum()
unichr()zip()
内建函数的使用
>>> s = 'foobar'
>>> for i, t in enumerate(s):
...     print i, t
...
0 f
1 o
2 o
3 b
4 a
5 r
>>> s, t = 'foa', 'obr'
>>> zip(s, t)
[('f', 'o'), ('o', 'b'), ('a', 'r')]

1.1.3 字符串内建函数

太多了,用dir()内建函数可以查看 例如:

>>> dir('hello')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>

1.1.4 字符串的删除

del string

1.1.5 只适用于字符串的操作符

  • 字符串格式化符号
    格式化字符转换方式
    %c字符
    %rrepr()优先处理的字符串
    %sstr()优先处理的字符串
    %d/%i有符号十进制数
    %u无符号十进制数
    %o无符号八进制数
    %x/%X无符号十六进制数
    %e/%E科学计数法
    %f/%F浮点数
    %g/%G
    %%输出%
  • 格式化操作符辅助指令
    符号作用
    *定义宽度或者小数点精度
    -左对齐
    +在正数前面显示+
    <sp>在整数前面显示空格
    #八进制前面显示0,十六进制前面显示0x
    0显示数字的前面显示0而不是空格
    %%
    (var)映射变量(字典参数)
    m.nm最小宽度,n小数点后面的位数
>>> '%+d, %+d' % (1, -1)
'+1, -1'
>>> '%#o, %#x' %(12, 12)
'014, 0xc'
>>> '%03d' % 2
'002'
>>> '%-3d' % 3
'3  '

1.1.6 原始字符串:

用操作符 r 来表示 主要用途: 正则表达式引入re模块来说用原始字符串的作用

>>> import re
>>> m = re.search('\\[n]', r'Hello \n')
>>> m.group()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> m = re.search('\\\[n]', r'Hello \n')
>>> m.group()
'\\n'
>>> m = re.search(r'\\[n]', r'Hello \n')
>>> m.group()
'\\n'

1.1.7 字符串的不变性

直接上例子

>>> s = 'hello'
>>> s
'hello'
>>> id(s)
3072956288L
>>> s += 'world!'
>>> s
'helloworld!'
>>> id(s)
3072937632L
>>> s[1]
'e'
>>> s[1] = 'c'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

1.1.8 TODO unicode

1.1.9 与字符串相关的模块

模块描述
string
re
struct字符串与二进制转换
c/StringIO字符串缓冲对象,操作类似于file对象
base26base 16 32 64 数据编解码
codecs解码器注册和基类
crypt进行单方面加密
difflib找出序列间的不同
hashlib各种不同安全hash算法和信息摘要算法的api
hmaHMAC信息鉴权算法的python实现
md5RSA的md5信息摘要鉴权
rotor提供多平台的加密解密服务
shaNIAT的安全hash算法
stringprep提供ip协议的unicode字符串
textwrap文本包装和填充
unicodedataunicode数据库

1.1.10 python不是一‘nul’结束的

在字符串中,我们只需要关注自己的内容即可。其余的事情python已经解决了。

1.2 列表

1.2.1 与字符串相同的性质

创建,访问(切片), 删除,成员操作符(in, not in), 链接符(+) 重复操作符(*)

1.2.2 列表解析

很重要,很实用。

1.2.3 内建函数

函数作用
len()
max()
min()
sorted()
reserved()
enumerate()
zip()
sum()
list()
tuple()

1.2.4 列表的内建函数

很多,可以用dir(list)观看。

1.3 元组

1.3.1 元组和列表的不同点

  1. 元组不可改变. 这一点决定了元组可以干许多列表不可以干的事情. 例如:做一个字典的key. 还有就是处理一组对象是,这个组默认为元组.
  2. 元组用(), 而列表用[].

1.3.2 元组不可变, 但是可用list() 转化为列表.

1.3.3 元组不可变性中的可变部分.

  1. 重新赋值法.
    >>> t = ('hello')
    >>> t
    'hello'
    >>> t += ' world!'
    >>> t
    'hello world!'
    
  2. 改变元组元素的元素法.
    >>> t = (['hello', 3], [' world!'], 1)
    >>> t
    (['hello', 3], [' world!'], 1)
    >>> t[0][1] = 2
    >>> t
    (['hello', 2], [' world!'], 1)
    

Date: 2014-01-27 一 至今

Author: hujianxin

Org version 7.9.3f with Emacs version 24

Validate XHTML 1.0