print('abc')
-
1.計算機的儲存形式:
計算機在存儲數(shù)據(jù)的時候馒过,都是以二進制的形式存在計算機中。
1.原碼:數(shù)據(jù)的二進制形式
'''
正數(shù)的原碼:符號位是0祸泪,后面是數(shù)值大小
負(fù)數(shù)的原碼:符號位的值是1吗浩,后面去掉符號的數(shù)值大小
'''
10-->1010 原碼:00001010
-10-->原碼 :10001010
2.反碼:
'''
正數(shù)的反碼:反碼和原碼一樣
負(fù)數(shù)的反碼:符號不變,后面每一位取反
'''
10-->反碼:00001010
-10-->反碼:11110101
3.補碼:
'''
正數(shù)的補碼:補碼和原碼一樣
負(fù)數(shù)的補碼:反碼+1
'''
10--> 補碼:00001010
-10--> 補碼:11110110
加法器:計算機只有加法碼,沒有辦法做減法
2+(-3)-->00000010+10000011+10000101(-5)原碼相加的結(jié)果
2+(-3)-->000000010+111111101=11111101=11111111(補碼)=10000001(-1)補碼相加
-
2. 什么是字符串:
什么是字符串
"""
a.用單引號或者雙引號括起來的字符集就算是字符串
例如:'faadi&7*^%%$#3栓傷口就會受到'
b.字符串中每個獨立的單元我們叫字符:例如:字符串'abc123'中的'a','b'
'c','1','2','3'就是字符
"""
name = '黃峰'
poem ='床前明月光'
2.轉(zhuǎn)義字符:
'''
說明:python中沒有字符類型没隘,如果要表示字符懂扼,就是用一個長度是1的字符串表示,例如;'a','1'
例如:值得就是字符串中字符的個數(shù),例如:'abc'-長度是3右蒲,'abc123,
你好'-長度是:10
通過\將一些特殊字符轉(zhuǎn)換成一個具有特殊功能或者特殊意義的字符阀湿,就是轉(zhuǎn)義字符。
常見的轉(zhuǎn)義字符:
\n---換行
\t---指標(biāo)建(相當(dāng)于tab鍵)
\---
'---'
"---"
在計算字符串長度的時候瑰妄,轉(zhuǎn)義字符的長度是1
'''
poem ='床前明月光陷嘴,\n疑是地上霜'
print(poem)
str1 = 'abc\bcd'
print(str1)
str2='abc'123'
print(str2)
str3="abc"123"
print(str3)
3.Unicode編碼
'''
python中字符的編碼采用的是Unicode編碼
將字符轉(zhuǎn)換成指定的數(shù)值,這個過程就是編碼间坐。(編碼的目的是方便計算機存儲)
將數(shù)值轉(zhuǎn)換成對應(yīng)的符號的過程就是反編碼(解碼)
Unicode是采用兩個字節(jié)對一個字符進行編碼(2^15),能夠?qū)⑹澜缟纤械姆栠M行編碼
Unicode編碼中包含了ascii碼
0-->0
'0'-->48
'abc'
100
'''
1).將Unicode碼轉(zhuǎn)換成字符: chr(編碼)
print(chr(0xA001))
print(chr(0xAC00))
print(chr(0x4e60))
將字符轉(zhuǎn)換成Unicode編碼:ord(字符)
code1 = ord('黃')
code2 = ord('峰')
print((code1),(code2))
-
3.獲取字符
字符串實質(zhì)可以是一個不可變的序列罩旋,序列內(nèi)容是字符。
一旦字符串確定眶诈,那么里面的字符和字符的位置就不變了,例如:'abc'
1.怎么獲取單個字符
'''
python中的字符串瓜饥,可以通過下標(biāo)(索引)來獲取指定位置上的字符:(字符串索引)
說明:
a.字符串可以是字符串值逝撬,也可以是字符串變量。
b.[]中使用固定語法
c.索引:從0開始到字符串長度減1(0對應(yīng)的是第一個字符)乓土;
字符串實質(zhì)可以是一個不可變的序列宪潮,序列內(nèi)容是字符。
一旦字符串確定趣苏,那么里面的字符和字符的位置就不變了狡相,例如:'abc'
1.怎么獲取單個字符
'''
python中的字符串,可以通過下標(biāo)(索引)來獲取指定位置上的字符:(字符串索引)
說明:
'''
a.字符串可以是字符串值食磕,也可以是字符串變量尽棕。
b.[]中使用固定語法
c.索引:從0開始到字符串長度減1(0對應(yīng)的是第一個字符);
-1~長度:(-1對應(yīng)最后一個字符彬伦,-2對應(yīng)倒數(shù)第二個字符)
注意:索引不能越界否則會報錯(產(chǎn)生異常)滔悉。
'''
str1='acbd'# a->0,c->1,b->2,d->3
print(str1[0])
print(str1[4]),# IndexError:string index out of range
name = '王海飛'
print (name[-1], name[-2],name[-3])
print ('abc'[2])
print(name[-4]) #IndexError:string index out of range
2.獲取部分字符(獲取子串)--切片
字符串【下標(biāo)1:下標(biāo)2】:從下標(biāo)1開始伊诵,獲取下標(biāo)2前的所有字符
(從下標(biāo)1開始,每次下標(biāo)值加1回官,一直加到下標(biāo)2前)
注意:下標(biāo)1對應(yīng)的位置曹宴,一定要在下標(biāo)2對應(yīng)的位置之前
str2 = 'hello world'
print(str2[0:4]) # hell
print(str2[2:7]) # llo w
print(str2[2:-1]) # llo worl
print(str2[3:12]) # lo word
print(str2[-5:-2]) # wor
print(str2[-5:9]) # wor
字符串[下標(biāo)注1:下標(biāo)注2:步進]
'''
從下標(biāo)1開始獲取,每次下標(biāo)值增加步進值歉提,每增加一次取一個字符笛坦,直到取到下標(biāo)2為止
a.步進如果是正數(shù),那么下標(biāo)1對應(yīng)的字符位置一定要在下標(biāo)2對應(yīng)位置的前面苔巨;
步進是負(fù)數(shù)的版扩,那么下標(biāo)1對應(yīng)的位置一定要在下標(biāo)2對應(yīng)的位置后面
b.下標(biāo)2對應(yīng)的字符是取不到的
'''
str3 = 'hellopython'
print(str3[0:5:2]) #hlo 步進 3 hl
print(str3[-1:5:-1]) # nohty
下標(biāo)的省略:
'''
切片的時候下標(biāo)1和下標(biāo)2可以省略的
下標(biāo)1省略:默認(rèn)從開頭開始獲取(開頭可以是字符串的第一個字符恋拷,也可能是字符串最后一個字符)
'''
str4 = 'good good study,day day up'
print(str4[:4]) # good
print(str4[:4:-1]) # pu yad yad,yduts doog
'''
下標(biāo)2省略:從下標(biāo)1開始獲取资厉,獲取到結(jié)束(結(jié)束可能是字符串的最后一個字符,也可能是字符串的最后一個字符)
'''
print(str4[1:]) # ood good study,day day up
print(str4[3::-1]) # doog
print(str4[:]) # good good study,day day up
print(str4[::-1]) # pu yad yad,yduts doog doog
練習(xí):要求將一個字符串中所有下標(biāo)是奇數(shù)位的字符獲取出來
str5 = 'abcdefg'
print(str5[1::2]) # bdf
-
4.字符串的相關(guān)運算:
1.+ 運算符
'''
字符串1+字符串2:
python 支持兩個字符串相加蔬顾,其效果是將兩個字符串拼接在一起形成一個新的字符串
注意:如果+一邊是字符串宴偿,那另外一邊也必須是字符串
'''
print('abc'+'123')
str1 = 'world'
newstr1 = 'hello'+' '+ str1
print(newstr1)
print(10+'abc') # TypeError:unsupported operand type(s) for +:'int' and 'str'
2.* 運算符:
'''
字符串1*整數(shù):字符串重復(fù)多次
'''
print('abc'*3)
3.所有的比較運算符
str1 = 'abc'
print('abc'== str1)
print(str1 != 'ab')
比較大小:
'''
str1 > str2;str1 < str2
讓str1中的每一位字符分別與str2中的每一位字符進行比較诀豁。
直到不同為止窄刘,再看不同字符中水的編碼值大或者小
'''
print('abcde'>'abcde') # False
print('二'>'余') # False
print(ord('二'),ord('余')) # 20108 20313
in和 not in:
'''
str1 in str2 :判斷str1是否在str2中(str2是否包含str1/str1是否是str2的子串)
結(jié)果是布爾值
'''
print('abc' in 'abcdef') # True
print('abc' in 'a1b2c3' ) # False
print('f' in 'python') # False
5.獲取字符串的長度:字符串的長度指的是字符串中字符的個數(shù)。
len()內(nèi)置函數(shù)
str3='project'
print(len(str3),len('123 abc/n')) # 7, 9
補充: 空串
str4 =''
str5 = ""
print(len(str4)) # 0
print(str3[-1],str3[len(str3)-1])# t t,-1 = len(str3)-1
6.阻止轉(zhuǎn)義:
在字符串的最前面添加r/R可以阻止轉(zhuǎn)義
print('a\nb','a\nb\')
print(len('a\nb\')) # 4
print(r'a\nb\',R'a\nb') # a\nb\ a\nb
print(len(r'a\nb\')) # 6
print('a\nb\\') # a\nb\
print('\\n\') # \換行
print(r'\\n\') # \\n\
練習(xí) :
str1 = r'\nabc123' 求:str1[3]: b
str2 = 'abc123\123' 求:str2[-5]:3
-# 5.字符串相關(guān)的方法:
python為字符串提供很多內(nèi)建函數(shù)
字符串.函數(shù)()
注意:這些所有函數(shù)的所有功能都不會影響原來的字符串舷胜,而是產(chǎn)生新的字符串
str1 = 'hello python'
- capitalize() 將字符串的第一個字符轉(zhuǎn)換成大寫
newstr1 = str1.capitalize()
print(newstr1, str1) # Hello python /hello python
print('abc'.capitalize())
2.center(width,fillchar)
讓字符串變成width的長度娩践,原內(nèi)容居中,剩余的部分用fillchar來填充
width--整數(shù)烹骨;fillchar--任意字符
print('abc'.center(10,'*')) # ***abc****
3.rjust(width,fillchar)
讓字符串變成width的長度翻伺,原內(nèi)容靠右,剩余的部分使用fillchar來填充.
1 2 3 10 11 12 --->001 002 003 010 011 012
1-->0001 11-->0011 123-->0123
number ='12'
new_id = number.rjust(4, '0')
print(new_id) # 0012
4.原字符串.count(str)
判斷str值在原字符串中出現(xiàn)的次數(shù)
str-->字符串
print('abccabaa'.count('ab')) # 2
print('貳1壹二2一'.isnumeric())
5.str1.join(str2)
在str2中的每個字符串之間插入一個str1
print('-+'.join('abc'))
6.str1.replace(old,new)
將str1中old全部替換成new
new_str = 'abcdahulapuyeahj'.replace('a','+')
print(new_str) # +bcd+hul+puye+hj