day11Python字符串魔法

day11Python字符串魔法
2、基本數據類型


數字  int ,所有的功能,都放在int裏
a1 = 123
a1 = 456

- int
將字符串轉換爲數字
a = "123"
print(type(a),a)


b = int(a)
print(type(b),b)

num = "0011" 
v = int(num, base=16)
print(v)
- bit_lenght
# 當前數字的二進制,至少用n位表示
r = age.bit_length()

字符串  str
###########################################


1 首字母大寫
test = "aLex"
v = test.capitalize()
print(v)


2 所有變小寫,casefold更牛逼,很多未知的對相應變小寫
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)


3 設置寬度,並將內容居中
20 代指總長度
*  空白未知填充,一個字符,可有可無
v = test.center(20,"中")
print(v)


test = "alex"
v = test.ljust(20,"*")
print(v)


test = "alex"
v = test.rjust(20,"*")
print(v)


test = "alex"
v = test.zfill(20)
print(v)




4 去字符串中尋找,尋找子序列的出現次數
test = "aLexalexr"
v = test.count('ex')
print(v)


test = "aLexalexr"
v = test.count('ex',5,6)
print(v)



encode
decode


5
以什麼什麼結尾
以什麼什麼開始
test = "alex"
v = test.endswith('ex')
v = test.startswith('ex')
print(v)


6 expandtabs,斷句20,
test = "username\temail\tpassword\nlaiying\[email protected]\t123\nlaiying\[email protected]\t123\nlaiying\[email protected]\t123"
v = test.expandtabs(20)
print(v)


7 從開始往後找,找到第一個之後,獲取其未知
> 或 >=
test = "alexalex"
未找到 -1
v = test.find('ex')
print(v)


8 index找不到,報錯   忽略
test = "alexalex"
v = test.index('8')
print(v)




9 格式化,將一個字符串中的佔位符替換爲指定的值
test = 'i am {name}, age {a}'
print(test)
v = test.format(name='alex',a=19)
print(v)


test = 'i am {0}, age {1}'
print(test)
v = test.format('alex',19)
print(v)


10 格式化,傳入的值 {"name": 'alex', "a": 19}
test = 'i am {name}, age {a}'
v1 = test.format(name='df',a=10)
v2 = test.format_map({"name": 'alex', "a": 19})


11 字符串中是否只包含 字母和數字
test = "123"
v = test.isalnum()
print(v)
str

12 是否是字母,漢子
test = "as2df"
v = test.isalpha()
print(v)


13 當前輸入是否是數字
test = "二" # 1,②
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)




14 是否存在不可顯示的字符
\t   製表符
\n   換行
test = "oiuas\tdfkj"
v = test.isprintable()
print(v)


15 判斷是否全部是空格
test = ""
v = test.isspace()
print(v)


16 判斷是否是標題
test = "Return True if all cased characters in S are uppercase and there is"
v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)
v3 = v2.istitle()
print(v3)


17 ***** 將字符串中的每一個元素按照指定分隔符進行拼接
test = "你是風兒我是沙"
print(test)
# t = ' '
v = "_".join(test)
print(v)


18 判斷是否全部是大小寫 和 轉換爲大小寫
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1, v2)


v1 = test.isupper()
v2 = test.upper()
print(v1,v2)
19
移除指定字符串
有限最多匹配
test = "xa"
# v = test.lstrip('xa')
v = test.rstrip('9lexxexa')
# v = test.strip('xa')
print(v)


test.lstrip()
test.rstrip()
test.strip()
去除左右空白
v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)
print(test)
去除\t \n
v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)


20 對應關係替換
test =  "aeiou"
test1 = "12345"


v = "asidufkasd;fiuadkf;adfkjalsdjf"
m = str.maketrans("aeiou", "12345")
new_v = v.translate(m)
print(new_v)


21 分割爲三部分
test = "testasdsddfg"
v = test.partition('s')
print(v)
v = test.rpartition('s')
print(v)


22 分割爲指定個數
v = test.split('s',2)
print(v)
test.rsplit()




23 分割,只能根據,true,false:是否保留換行
test = "asdfadfasdf\nasdfasdf\nadfasdf"
v = test.splitlines(False)
print(v)


24 以xxx開頭,以xx結尾
test = "backend 1.1.1.1"
v = test.startswith('a')
print(v)
test.endswith('a')


25 大小寫轉換
test = "aLex"
v = test.swapcase()
print(v)


26 字母,數字,下劃線 : 標識符 def  class
a = "def"
v = a.isidentifier()
print(v)




27 將指定字符串替換爲指定字符串
test = "alexalexalex"
v = test.replace("ex",'bbb')
print(v)
v = test.replace("ex",'bbb',2)
print(v)
##################### 7個基本魔法 ######################
join       # '_'.join("asdfasdf")
split
find
strip
upper
lower
replace
##################### 4個灰魔法 ######################
test = "鄭建文妹子有種衝我來"


一、for循環
for 變量名 in 字符串:
    變量名
break
continue


index = 0
while index < len(test):
    v = test[index]
    print(v)

    index += 1
print('=======')


for zjw in test:
    print(zjw)


test = "鄭建文妹子有種衝我來"
for item in test:
    print(item)
    break


for item in test:
    continue
    print(item)


二、索引,下標,獲取字符串中的某一個字符
v = test[3]
print(v)
ok = input('輸入密碼')
v=len(ok)
for a in range(0,len(ok)):
    print(a,ok[a])


三、切片
v = test[0:-1] # 0=<  <1
print(v)


四、獲取長度
Python3: len獲取當前字符串中由幾個字符組成
v = len(test)
print(v)


注意:
len("asdf")
for循環
索引
切片
字符串生成或者是修改會生成新的內存地址
1.python xx.py ./xx/xx.py
2.8位一個字節
3.不同的編碼格式
4.3 4
5.# '''    '''
6.聲明變量用於後面的參數使用 數字字母下劃線
7.bit_length()
8.True False
9.alex Alex 
""是假的
" "真
0 真
其他 真 
10.
21.字符串是不可以迭代的對象,for a in item():
22.
23.一個先創建,一個後創建
24.
25.
26.int 整形 srt  字符串類型 類和對象的關係
27.format 類的使用
28、製作隨機驗證碼,不區分大小寫。
流程:
-­‐
用戶執行程序
-­‐
給用戶顯示需要輸入的驗證碼
-­‐
用戶輸入的值
用戶輸入的值和顯示的值相同時現實正確信息;否則繼續生成隨機驗證碼繼續等待用戶輸入
生成隨機驗證碼代碼示例:
29.
29、開發敏感詞語過濾程序,提示用戶輸入內容,如果用戶輸入的內容中包含特殊的字符:

"蒼老師"
"東京熱",則將內容替換爲
***


v='jsjg'
v=v.replace('s','xxxxx')
v=v.replace('s','xxxx')
30、製作表格
循環提示用戶輸入:用戶名、密碼、郵箱
(要求用戶輸入的長度不超過20 個字符,如果超過則只有前20 個字符有效)
如果用戶輸入
q 或Q
表示不再繼續輸入,將用戶輸入的內容以表格形式大隱
expandtabs

len

                                                [0,20]

import os


# -*- coding: UTF-8 -*-
# n=0
# while n<3:
#     n1=input('user')
#     n2=input('pw')
#     if n1=='ouyang'and n2=='pw':
#         print('ok')
#     else:
#         print("fail")
#     n=n+1
# name='abcd'
# if 'na' in name:s
#     print("ok")
# else:
#     print('false')

# a='123'
# print(type(a))
# print(a)
# b=int(a)
# print(type(b))
# print(b)
# num='0011'
# v=int (num,base=2)
# print(v)
# test='ales'
# a=str(test)
# print(a)
# v=test.encode(encoding='utf-8')
# print(v)
# ac = 'i am {name}'
# print(test)
# v = ac.format(name='alex')
# print(v)
# 6 expandtabs,斷句20# test = "username\temail\tpassword\nlaiying\[email protected]\t123\nlaiying\[email protected]\t123\nlaiying\[email protected]\t123"
# v = test.expandtabs(20)
# print(v)
# test='aledg'
# v=test.swapcase()#大小寫轉換
# print(v)
# v1=test.join('xq')
# print(v1)
# test = "你是風兒我是沙"
# print(test)
# # t = ' '
# v = "_".join(test)
# print(v)
# ok = input('輸入密碼')
# v=len(ok)
# for a in range(0,len(ok)):
#     print(a,ok[a])
#

# name='aleX'
# a1=name.lstrip()
# a2=name.startswith('al')
# a3=name.endswith('X')
# a4=name.replace('l','p')
# a5=name.split('l')
# a6=name.upper()
# a7=name.lower()
# a8=name[2]
#
# for a in range(3):
#     print(name[a])
# print(name[len(name)-1])
# print(name[len(name)-2])
# a9=name.index('e')
# print(a9)
# a10='asdfer'
# a11=a10.count('asdfe',0,len(a10)-1)
# print(name,a11)
# li='alexericrain'
# li=['alex',
# 'eric',
# 'rain']
# a12='_'.join(li)
# print(a12)
# count1=int(input('輸入數字'))
# count2=int(input('第二個數'))
# coun=count1+count2
# print(coun)

# coun3='asduiaf878123jkjsfd-­‐213928'
# num1=coun3.isalnum()
# num2=coun3.isalpha()
# print(num1,num2)

# as1=input('名字')
# as2=input('地點')
# as3=input('事情')
# print("%s的家鄉在 %s,學習啦%s"%(as1,as2,as3))

def check_code():
    import random
    checkcode = ''
    for i in range(4):
        current = random.randrange(0, 4)
        if current != i:
            temp = chr(random.randint(65, 90))
        else:
            temp = random.randint(0, 9)
        checkcode += str(temp)
    return checkcode


code = check_code()
print(code)
a = input("輸入你想要的算法:")

v1, v2 = a.split('+')

c1 = 0
c2 = 0
val = input(">>>")
for itme in val:
    if itme.isalnum():
        c1 += 1
    if itme.isalpha():
        c2 += 1
gg = '{0}的家鄉在{1},喜歡{2}'
name=input("輸入")
v=gg.format('uyang' ,18)

while True:
    code = check_code()
    print(code)
    v=input(">>>")
    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章