1. 程式人生 > >Python學習之二(字串的操作)

Python學習之二(字串的操作)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#"做除法運算"

#in /not in 用來判斷某個字元是否在某個字串裡面成員運算
name="中科大"
if "科" in name:
    print('OK')
else:
    print('Error')

if "中" not in name:
    print('OK')
else:
    print('Error')

#驗證v的布林值是否正確
v="真" not in name
print(v)

a=1==2
print(a)

#and 和 or 的判斷優先順序 這裡的判斷原則和順序和C中沒有多大差別
user = 'alex'
passwd = '123'
v = user=='alex' and passwd=='123' or 1==1 and passwd=='456'
print(v)

#基本資料型別:數字,字串,布林值bool,字典dict,列表 list,元組 tuple
#Python3裡,不管數字多大數字都是int型別
num=123
v=num.bit_length()
print(v)

name1='zhongkeda'
v1=name1.upper()
print(v1)
name2='hegongda'
v2=name2.upper()
print(v2)

#進位制之間的轉換,base後面是幾就是幾進位制
num = '0011'
v=int(num,base=2)
print(v)

age = 10
r=age.bit_length()
print(r)

test = 'alex'
v=test.capitalize()
print(v)
v1=test.casefold()
print(v1)
v2=test.lower()
print(v2)
#設定寬度,並將內容居中,其他內容用*填充
v=test.center(20,'*')
print(v)

#計算字串中某種字元出現的次數
test='alexalexr'
v=test.count('a',3)
print(v)

#表示以是否以某個字元結尾,某個字元開頭
test='alex'
v=test.endswith('x')
v1=test.startswith('a')
print(v)
print(v1)

#從開頭往後找,找到第一個返回陣列下標,可以設定查詢範圍
test='alexalex'
c= test.find('x',5,8)
print(c)


#將字串中的佔位符轉換為實際值
text = 'i am {name},age {a}'
print(text)
v=text.format(name='alex',a=19)
print(v)

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

#判斷是否是否只是數字和字母
test = 'uasf890'
v=test.isalnum()
print(v)

#兩個的功能都是用來判斷是否是數字,但是isdigit範圍更廣
test = '②'
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)


#二十個一組,遇到tab,用空格將六個補齊
test ='username\temail\tpassword\nlolun\
[email protected]
\t123\nluckly\[email protected]\t567' v=test.expandtabs(20) print(v) #列印時是否能輸出真實的值 test = 'ouid\nkej' v = test.isprintable() print(v) #判斷是否是空字串 test = ' ' v = test.isspace() print(v) #判斷是否是標題,並把不是標題的轉成標題 test = 'Process finished with exit code 0' v1=test.istitle() print(v1) v2=test.title() print(v2) # #將字串中的每一個元素按照指定分隔符進行拼接 test = '離離原上草' print(test) t=' ' v=t.join(test) print(v) #指定字元長度並往裡填充自定義字元 test='alex' v1=test.ljust(20,'*') print(v1) v2=test.rjust(20,'*') print(v2) #判斷是否是大小寫,把字元轉換成大小寫 test = 'Alex' v1=test.islower() v2=test.lower() print(v1,v2) v3=test.isupper() v4=test.upper() print(v3,v4) #去除指定字元也包括子序列,預設去除左邊和右邊的空白,也可以去除\n \t test='alex' v1=test.lstrip('a') v2=test.rstrip('x') v3=test.strip('a') v4=test.strip('aaaex') print(v1,v2,v3,v4) #對應關係的替換 test1='ausdfehjkl' test2=str.maketrans('aoeiu','12345') test3=test1.translate(test2) print(test3) test = 'testasdfqwers' print(test.partition('s')) #只能分割第一個匹配的字串 print(test.rpartition('s')) #分割最後一個字串 print(test.split('s')) #預設所有的都可以分,可以定義分割次數 print(test.split('s',2)) #但是分割後的s拿不到 test = 'asdfasfd\njkllkj\n' v = test.splitlines(False) #按照換行符進行分割,後面跟上是否要保留\n print(v) #大小寫轉換 test = 'aLex' print(test.swapcase()) test = 'alex' v=test[0:2] #獲取字串中的從第0個開始到第2個,不包括2 print(v) print(len(test)) #獲取字串裡字元的個數 li = [1,2,3,4,5,6,7] print(len(li)) test = '人生得意須盡歡莫使金樽空對月' index = 0 while index<len(test): v = test[index] print(v) index+=1 #迴圈每個元素的變數名,輸出每個元素 for ggg in test: print(ggg) #字串裡的字元替換,後面跟的數字是替換的個數 test = 'alexalexalex' print(test.replace('ex','ww',2)) #字串一旦建立就不能修改,如果要修改就在記憶體中重新建立一個字串 #建立連續的數字0-99,最後一個數字用來指定步長 v = range(0,100,5) for item in v: print(item) #列印文字對應的索引 test =input(">>>") print(test) r=range(0,len(test)) for item in r: print(item,test[item])