什麼是數據類型:python
咱們人類能夠很容易的分清數字與字符的區別,可是計算機並不能呀,計算機雖然很強大,但從某種角度上看又很傻,除非你明確的告訴它,1是數字,「漢」是文字,不然它是分不清1和‘漢’的區別的,所以,在每一個編程語言裏都會有一個叫數據類型的東東,其實就是對經常使用的各類數據類型進行了明確的劃分,你想讓計算機進行數值運算,你就傳數字給它,你想讓他處理文字,就傳字符串類型給他。git
info = ''' ------------ info of %s ----------- Name : Coy Yang Age : 100 job : Teacher Hobbie: girl ------------- end ----------------- ''' print(info)
佔位符%及雙%%編程
print('我叫%s,今天%d,學習進度2%%' % ('Coy',25))
字符格式化輸出api
佔位符 編程語言
優先級學習
print(1 > 2 and 3 < 4 or 2 > 1 and 4 < 3)
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)
print(3 or 4) print(1 or 4) print(0 or 1) print(3 and 5)
s1 = 'yangchen' s2 = "yangchen" msg = "My name is Coy, I'm 25 years old!" print(msg)
三引號通常都是在長字符串時候引用this
msg = ''' 今天我想寫首小詩, 歌頌個人同桌, 你看他那烏黑的短髮, 好像一隻炸毛雞。 ''' print(msg)
2,字符串+*spa
(1)+3d
s1 = 'coy' s2 = 'ssss' print(s1+s2)
(2)*code
s1='coy' print(s1*8)
1,字符串索引切片
經過索引切出來的是新字符串,新的內存地址,在取的時候要多取一個,由於顧頭不顧尾
s = 'coysaihanhan' s1 = s[0] print(s1) #獲取字符串索引爲0的 s = 'coysaihanhan' s2 = s[2] print(s2) #獲取字符串索引爲2的 s = 'coysaihanhan' s3 = s[-1] print(s3) #從後往前,取倒數第一個 s = 'coysaihanhan' s4 = s[-2] print(s4) #從後往前,取倒數第二個 s = 'coysaihanhan' s5 = s[0:3] print(s5) #從索引爲0的開始取到索引爲2的,由於顧頭不顧尾 s = 'coysaihanhan' s5 = s[0:4] print(s5) #從索引爲0的開始取到索引爲3的,由於顧頭不顧尾 s = 'coysaihanhan' s5 = s[:4] print(s5) #從索引爲0的開始取到索引爲3的,由於顧頭不顧尾,:前不加默認是最前 s = 'coysaihanhan' s5 = s[4:9]#aihan print(s5) #從索引爲4的開始去到索引爲8的,由於顧頭不顧尾 s = 'coysaihanhan' s5 = s[:] print(s5) #從頭至尾 #能夠加步長 #隔一個取一個,默認1是挨着,2是隔一個取一個 s = 'coysaihanhan' s5 = s[:5:2] print(s5)#cya #反向步長 #倒着取必須加反向步長 s = 'coysaihanhan' s5 = s[-1:-5:-1] print(s5)#nahn
總結:s[起始索引:結束索引+1:步長]
2,str操做
(1)capitalize
首字母大寫其他字母小寫
a = 'coyyangCHENhanhan' s1 = a.capitalize() print(s1)
print結果爲:
Coyyangchenhanhan
(2)swapcase
大小寫翻轉
a = 'coyyangCHENhanhan' s2 = a.swapcase() print(s2)
print結果爲:
COYYANGchenHANHAN
(3)title
非字母元素隔開的每一個單詞首字母大寫
a = 'coy yang*chen2hanhan' s3=a.title() print(s3)
print結果:
Coy Yang*Chen2Hanhan
(4)center
總長度30而且讓字符串居中
a = 'coyyangchenhanhan' s4=a.center(30) print(s4)
print結果爲:
能夠經過源代碼來查看一些參數
查看源代碼後發現有默認參數,證實能夠添加一個參數,這裏默認是None
a = 'coyyangchenhanhan' s4=a.center(30,'*') print(s4)
print結果:
(5)upper和lower
upper全大寫,驗證碼時候用
a = 'coyyangchenhanhan' s5=a.upper() print(s5)
print結果:
COYYANGCHENHANHAN
code = 'AwEqe' username = input('請輸入用戶名') you_code = input('請輸入驗證碼') if username == 'coy' and you_code.upper() == code.upper(): print('驗證成功')
lower全小寫
a = 'COYYANGCHENHANHAN' s5=a.lower() print(s5)
coyyangchenhanhan
(6)startswith和endswith
a = 'coyyangchenhanhan' s6 = a.startswith('c') print(s6)
print結果:
a = 'coyyangchenhanhan' s6 = a.startswith('coy') print(s6)
print結果
來看源碼,有兩個默認參數,默認都是None
設置字符串的起始位置和結束位置
str = "this is string example....wow!!!" print (str.startswith( 'this' )) print (str.startswith( 'string', 8 )) print (str.startswith( 'this', 2, 4 ))
print結果:
endswith
以什麼什麼爲結尾
a = 'coyyangchenhanhan' s6 = a.endswith('n') print(s6)
print結果
str = "this is string example....wow!!!" suffix = "wow!!!" print(str.endswith(suffix)) print(str.endswith(suffix, 20)) suffix = "is" print(str.endswith(suffix, 2, 4)) print(str.endswith(suffix, 2, 6))
print結果:
(7)strip
a = ' coyyangchenhanhan\t' s7 = a.strip() print(s7)
print結果:
coyyangchenhanhan
例子:
username = input('請輸入用戶名').strip() password = input('請輸入密碼') if username == 'coy' and password == '123': print('登陸成功')
ss = 'coyyangchenhanhan' ss1 = ss.strip('cn') print(ss1)
(8)replace
替換
首先看源代碼
s = 'coy wgrwa coy' s8 = s.replace('coy','han',1) print(s8)
print結果:
han wgrwa coy
(9)split
s = 'coy yangchen hanhan' l = s.split() print(l)
print結果:
['coy', 'yangchen', 'hanhan']
也能夠定義任何元素分割
s = 'coy,yangchen,hanhan' l1 = s.split(',') print(l1)
print結果:
['coy', 'yangchen', 'hanhan']
s = 'coyyangchenhanhan' l2 = s.split('c') print(l2)
print結果:
['', 'oyyang', 'henhanhan']
也能夠按照次數分割
s = 'coyyangchenhanhan' l3 = s.split('h') print(l3)
print結果:
['coyyangc', 'en', 'an', 'an']
按照次數分割後
s = 'coyyangchenhanhan' l3 = s.split('h',2) print(l3)
print結果:
['coyyangc', 'en', 'anhan']
(10)join list-------->str
s = ['coy','yangchen','hanhan'] s10 = ''.join(s) print(s10)
print結果:
coyyangchenhanhan
也能夠用空格來鏈接
s = ['coy','yangchen','hanhan'] s10 = ' '.join(s) print(s10)
print結果:
coy yangchen hanhan
可是必須列表裏必須是字符串不然報錯
(11)find,index
經過元素找索引
s = 'coyyangchenhanhan' s11 = s.find('c') s12 = s.find('coy') print(s11) print(s12) s13 = s.find('c',1) print(s13)
print結果:
index
s = 'coyyangchenhanhan' s11 = s.index('c') s12 = s.index('coy') print(s11) print(s12) s13 = s.index('c',1) print(s13)
區別在於,find沒找到就-1 ,index沒找到就報錯
(12)len
len 測量個數
s = 'coyyangchenhanhan' print(len(s))
print結果:
17
(13)count
元素出現的個數
s = 'coyyangchenhanhan' print(s.count('h'))
print結果:
17
(14)format
格式化三種用法
第一種 s = "我叫{},今年{},愛好{}" s12 = s.format('coy','25','hanhan') print(s12) 第二種 s = "我叫{0},今年{1},愛好{2},我依然叫{0}" s13 = s.format('coy','25','hanhan') print(s13) 第三種 s = "我叫{name},今年{age},愛好{hobby}" s14 = s.format(age=25,hobby='hanhan',name='coy') print(s14)
(15)is系列
name='jinxin123' print(name.isalnum()) #字符串由字母或數字組成 print(name.isalpha()) #字符串只由字母組成 print(name.isdigit()) #字符串只由數字組成