BASE64对字符串编码和解码 (2009-7-26)
a= "this is a teat"
b = base64.encodestring(a)
print b
>>>
dGhpcyBpcyBhIHRlYXQ=
print base64.decodestring(b)
>>>this is a teat
变量类型
Python是有变量类型的,而且会强制检查变量类型。
内置的变量类型有如下几种:
#整型
integer_number = 90
#浮点
float_number = 90.4
#复数
complex_number = 10 + 10j
#list 序列
sample_list = [1,2,3,'abc']
#dictionary 字典
sample_dic = {"key":value, 2:3}
#tuple 只读的序列
sample_tuple = (1,3,"ab")
字符串用单撇号或双撇号包裹
撇号和其它特殊字符用用反斜杠转义。
如果字符串中有单撇号而没有双撇号则用双撇号包裹,否则应该用单撇号包裹。
print 语句可以不带撇号或转义输出字符串。
字符串可以用+号连接起来,用*号重复
>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
>>>
字符串可以象在C 中那样用下标索引,字符串的第一个字符下标为0。
>>> word[4]
'A'
>>> word[0:2]
'He'
>>> word[2:4]
'lp'
>>>
片段有很好的缺省值:第一下标省略时缺省为零,第二下标省略时缺省为字符串
的长度。
>>> word[:2] # 前两个字符
'He'
>>> word[2:] # 除前两个字符串外的部分
'lpA'
>>>
下标允许为负数,这时从右向左数。例如:
>>> word[-1] # 最后一个字符
'A'
>>> word[-2] # 倒数第二个字符
'p'
>>> word[-2:] # 最后两个字符
'pA'
>>> word[:-2] # 除最后两个字符外的部分
'Hel'
>>>
内置函数len()返回字符串的长度
多行的长字符串也可以用行尾反斜杠续行,续行的行首空白不被忽略
三重撇号,三重撇号字符串也可以用三个单撇号
hello = """
This string is bounded by triple double quotes (3 times ").
Unescaped newlines in the string are retained, though /
it is still possible/nto use all normal escape sequences.
Whitespace at the beginning of a line is
significant. If you need to include three opening quotes
you have to escape at least one of them, e.g. /""".
This string ends in a newline.
"""