美文网首页
Python three day

Python three day

作者: Adapa | 来源:发表于2017-10-24 18:51 被阅读9次

字符串:

join()方法拼接

a='123'
b='258'
c="-".join([a,b])
c = '123-258'

count()数字符串中存在某个字符的个数

a='hellw kitty'
print(a.count('t'))

capitalize()首字母大写

a='hellw kitty'
print(a.capitalize())

center()补充强调

a='hellw kitty'
print(a.center(50,'#'))
>>>###################hellw kitty####################   #这个全部加起来为50个

endswith()判断以什么结尾

a='hellw kitty'
print(a.endswith('tty'))
>>>True

startswith()判断以什么开头:

a='hellw kitty'
print(a.startswith('he'))
>>>True

expandtabs控制空格数

a='hellw\t kitty'
print(a.expandtabs(tabsize=20))
>>>hellw                kitty

find查找到第一个元素并将索引返回
没有找到返回-1

a='hellw kitty'
print(a.find('t'))
>>>8

encode()编码器 & decode()解码器
encode() 方法以指定的编码格式编码字符串。

str = "菜鸟教程";
str_utf8 = str.encode("UTF-8")
str_gbk = str.encode("GBK")

print(str)

print("UTF-8 编码:", str_utf8)
print("GBK 编码:", str_gbk)

print("UTF-8 解码:", str_utf8.decode('UTF-8','strict'))
print("GBK 解码:", str_gbk.decode('GBK','strict'))

format()

a='hellw kitty for {bibi} is {age}'
print(a.format(bibi='sadasd',age='60'))
>>>hellw kitty for sadasd is 60

format_map()
传字典

a='hellw kitty for {bibi} is {age}'
print(a.format_map({'bibi':'hehe','age':32}))
>>>hellw kitty for hehe is 32

isalnum()
是否包含数字或者字母

print('sadas4564'.isalnum())
>>>True

isidentifier()
是否是非法变量

strip() --------……------------ --------……------------------……----------
去掉换行符和空格

rstrip()去掉右边
lstrip()去掉左边

replace('选择替换的内容',‘填补内容’,替换次数)

print('Hello world'.replace('world','OK'))
>>>Hello OK

split()
通过什么分割,返回list

print('Hello world ok'.split(' '))
>>>['Hello', 'world', 'ok']

摘一些重要的字符串方法

#1 print(st.count('l'))
# print(st.center(50,'#'))   #  居中
# print(st.startswith('he')) #  判断是否以某个内容开头
# print(st.find('t'))
# print(st.format(name='alex',age=37))  # 格式化输出的另一种方式   待定:?:{}
# print('My tLtle'.lower())
# print('My tLtle'.upper())
# print('\tMy tLtle\n'.strip())
# print('My title title'.replace('itle','lesson',1))
# print('My title title'.split('i',1))

小练习:

程序: 三级菜单
要求:
打印省、市、县三级菜单
可返回上一级
可随时退出程序

city={
    "北京":{
        "海淀":{
            '五道口':{
              'sohu':{},
                'google':{},
                '网易':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{}
            }

        },
        '昌平':{
            '沙河':{
                '北航':{},
                '孬奈还':{}
            },
            '天通苑':{},
            '回龙观':{}
        }
    },
    "成都":{
        '金牛区':{
            '四川大学':{},
            '华西大学':{}
        },
        '成华区':{
            '电子科大':{},
            '理工大':{}
        },
        '高新区':{
            '麦子学院':{},
            '小楠学院':{},
        },
    }
}
jjs=False
jsj=False
while not jjs and not jsj:
    for key in city:
        print(key)
    input1=input('01>>>:').strip()
    if(input1 == 'q'):
        jsj = True
    if input1 in city:
        while not jjs and not jsj:
            for key2 in city[input1]:
                print(key2)
            input2=input('02>>>:').strip()
            if input2 == 'b':
                jjs = True
            elif (input2 == 'q'):
                jsj = True
            if input2 in city[input1]:
                while not jjs and not jsj:
                    for key3 in city[input1][input2]:
                        print(key3)
                    input3=input('03>>>:').strip()
                    if input3 =='b':
                        jjs=True
                    elif(input3=='q'):
                        jsj=True
                else:
                    jjs=False
        else:
            jjs = False

优化版本:

city={
    "北京":{
        "海淀":{
            '五道口':{
              'sohu':{},
                'google':{},
                '网易':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{}
            }

        },
        '昌平':{
            '沙河':{
                '北航':{},
                '孬奈还':{}
            },
            '天通苑':{},
            '回龙观':{}
        }
    },
    "成都":{
        '金牛区':{
            '四川大学':{},
            '华西大学':{}
        },
        '成华区':{
            '电子科大':{},
            '理工大':{}
        },
        '高新区':{
            '麦子学院':{},
            '小楠学院':{},
        },
    }
}
menu = city
bibi=[]
while True:
    for key in menu:
        print(key)
    inputq = input('>>>:').strip()
    if len(inputq) == 0:continue
    if inputq in menu:
        bibi.append(menu)
        menu = menu[inputq]
    elif inputq=='q':
        break
    elif inputq=='p':
        if menu ==city:
            break
        else:
            menu=bibi.pop()
    else:
        print('Print Wernng!')

编码:

二进制:
---->ASCII:只能英文and拉丁字符。一个字符占一个字节8位
----->unicode:utf-32:占4个字节
----->unicode:utf-16:占2个字节或者两个以上,65535
------>unicode:utf-8:一个英文用ASCII码占一个字节,一个中文占三个字节,占位不一。

相关文章

网友评论

      本文标题:Python three day

      本文链接:https://www.haomeiwen.com/subject/egheuxtx.html