python练习
1.#!/usr/bin/python
#-- coding: UTF-8 --
#设置中文编码
#注意行缩进,否则执行时会报错
print(“自己练习python!”)
#练习交换两个变量
a = “change”
b = “mySelf”
c = “342253”
d = “hello_world_python”
e = “We are happy.”
#c= “abdba”
def changeAB(a,b):
print(“交换之前,第一个变量为:”+a+",第二个变量为:"+b)
temp = a
a = b
b = temp
print(“交换之后,第一个变量为:”+a+",第二个变量为:"+b)
#changeAB(a,b)
#练习判断回文
def huiWen©:
temp1 = len©#len©得到的是字符串c的长度
temp = temp1%2
resultStr = “”
if temp ==0:
#代表是偶数
temp2 = int(temp1/2)#c字符串长度的一半
temp3 = len©-1
for i in range(0,temp2):
temp4=temp3-i
if c[i]==c[temp4]:
#print(“true”)
resultStr = resultStr+"_true"
else:
#print(“false”)
resultStr = resultStr+"_false"
if “false” in resultStr:
print(“字符串”+c+“不是回文!”)
else:
print(“字符串”+c+“是回文…”)
else:
#代表是奇数
#print(temp1)
temp2 = int((temp1-1)/2)
#print(temp2)
temp3 = len©-1
#print(temp3)
for i in range(0,temp2):
temp4 = temp3-i
if c[i]==c[temp4]:
#print(“true”)
resultStr = resultStr+"_true"
else:
#print(“fasle”)
resultStr = resultStr+"_false"
if “false” in resultStr:
print(“字符串”+c+“不是回文!”)
else:
print(“字符串”+c+“是回文…”)
#huiWen©
#print(len©)
#练习字符串切割
def ziFuChuanQieGe(d):
temp = d.split(’_’)
#print(temp)
return temp
#ziFuChuanQieGe(d)
def pinJie(d):
list = ziFuChuanQieGe(d)
temp = “”
print(list)
for i in range(0,len(list)):
temp =temp+list[i]+"_"
print(temp)
#print(len(tempStr))
str1 = len(temp)-1
print(temp[0:str1])
#pinJie(d)
#练习替换字符串
def tiHuan(e):
temp = “”
for i in range(0,len(e)):
if e[i]==" “:
temp = temp+”%20"
else:
temp=temp+e[i]
#print(temp)
return temp
#t1 = tiHuan(e)
#print(t1)