定义函数体,基本概念不做介绍
全局和局部变量
函数体里的变量是局部的,可以在函数内部使用,而函数体外部的变量是全局的,可以在函数体内部访问(读),但如果想在函数内部修改全局变量得先用global声明一下要修改的全局变量。
x = 12def m():global xx = 13print xm()print x
x = 12def m():#global xx = 13print xm()print x
一个声明了全局变量,改变了x的值,而另一个没有声明,则还是局部变量,因此第一个x结果为13,第二个x为12
字符串的一些骚操作:
from string import Template
s = Template(‘$who likes $what’)
who =[“jim”,”tom”,”jack”,”hans”]
what = [“rice”,”noddle”,”cake”,”chicken”]
for i in range(len(who)):
print(s.substitue(who = who[i],what = what[i]))
(2) string.find()
(3) string.index(s)
(4) string.count(s)
(5) string.split(s,’,’)
(6) string.join()
(7)string.replace(“old”,”new”)
(8)S.strip([chars])strip函数可以去掉字符串首尾的某些字符(不是字符串),又称剥离函数,语法格式如下所示
s = "%!&pyt&%!hon&%!"c = "!%&"t = s.strip(c)print (t)
