2021-04-20,python习题练习

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(c):
    temp1 = len(c)#len(c)得到的是字符串c的长度
    temp = temp1%2
    resultStr = ""
    if temp ==0:
        #代表是偶数
        temp2 = int(temp1/2)#c字符串长度的一半
        temp3 = len(c)-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(c)-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(c)
#print(len(c))    
#练习字符串切割
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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值