2018-08-22day3筆記

認(rèn)識字符串

1.什么是字符

a. 用單引號或者雙引號括起來的字符就是字符傳
例如 :

'yeyu' "葉玉" 'xiaoyezi'     
name='yeyu'
poem="床前明月光"
2.轉(zhuǎn)意字符

說明:python 中沒有字符類型,如果要表示字符,就是用一個長度是1的字符串表示,例如:'a','1',
長度:指的是字符串中的個數(shù),例如: 'abc' 長度是3,'abc 123,你好'長度是:10
a.通過\將一些特殊的字符轉(zhuǎn)換成一個具有特殊意義的字符,就是轉(zhuǎn)義字符

b. 常見的轉(zhuǎn)義字符:

\n --換行
\t --制表符(相當(dāng)于tab  健)
\\ --\
\' --'
\"--"

在計算字符串長度的時候,轉(zhuǎn)義字符的長度是1

xiaoyezi='123\n456'
print(xiaoyezi)
123
456

yeyu='123,\t456'
print(yeyu)
123,  456

str1 = 'abc\\scd'
print(str1)
abc\scd

str2 = 'abc\'123"abc'
print(str2)
abc'123"abc

str3 = "abc\"123"
print(str3)
abc"123
3.Unicode編碼

a.python中字符的編碼采用的是Unicode編碼

b.Unicode是采用兩個字節(jié)對一個字符進行編碼(2^15),能夠?qū)⑹澜缟纤械姆栠M行編碼

c.Unicode編碼中包含了ascii碼

將數(shù)值轉(zhuǎn)換成對應(yīng)的符號的過程就是反編碼(解碼)
0 --> 0
'0' --> 48

# 1).將Unicode碼轉(zhuǎn)換成字符: chr(編碼)
print(chr(0xA001))
print(chr(0xAC00))
print(chr(0x4e60))

# 2).將字符轉(zhuǎn)換成Unicode編碼: ord(字符)
code1 = ord('余')  # 結(jié)果是10進制的
code2 = ord('婷')
print(hex(code1), hex(code2))

字符串實質(zhì)可以是一個不可變的序列,序列內(nèi)容是字符。
一旦字符串確定,那么里面的字符和字符的位置就不可變了,例如:'abc'

1.怎么獲取單個字符

'''
python中的字符串,可以通過下標(biāo)(索引)來獲取指定位置上的字符:字符串[索引]
說明:
a.字符串:可以是字符串值,也可以是字符串變量
b.[]: 中括號使固定語法
c.索引:從0開始到字符串長度減1 (0對應(yīng)第一個字符);
-1 ~ -長度(-1對應(yīng)的是最后一個字符, -2對應(yīng)的是倒數(shù)第2個字符)

注意:索引不能越界,否則會報錯(產(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])
print(name[-1], name[-2], name[-3])
print('abc'[2])
print(name[-4]) # IndexError: string index out of range

  1. 獲取部分字符(獲取子串) -- 切片
    字符串[下標(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]) # 切片時下標(biāo)可以越界,越界的時候就取臨界值
print(str2[-5:-2])
print(str2[-5:9])
字符串[下標(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)的位置的前面;
步進是負數(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])
print(str4[:4:-1])

"""
下標(biāo)2省略: 從下標(biāo)1位置開始獲取,獲取到結(jié)束(結(jié)束可能是字符串的最后一個字符,也可能是字符串的第一個字符)
"""
print(str4[1:])
print(str4[3::-1])

print(str4[:]) # good good study,day day up
print(str4[::-1]) # pu yad yad,yduts doog doog

練習(xí):要求將一個字符串中所有下標(biāo)是奇數(shù)位上的字符獲取出來

str5 = 'abcdefgh'
print(str5[1::2]) # 1. + 運算符
"""
字符串1+字符串2:
python支持兩個字符串相加, 其效果就是將兩個字符串拼接在一起產(chǎn)生一個新的字符串

注意:如果+的一邊是字符串,那么另外一個也必須是字符串
"""
print('abc'+'123')
str1 = 'world'
newstr = 'hello' + ' ' + str1
print(newstr)

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('dbcd' > 'abcde')
print('二' > '余婷')
print(ord('二'), ord('余'))

4.in 和 not in

"""
str1 in str2: 判斷str1是否在str2中(str2是否包含str1 / str1是否是str2的子串)

結(jié)果是布爾值
"""
print('abc' in 'a1b2cdefg')
print('f' not in 'python')

5.獲取字符串長度

字符串的長度,指的是字符串中字符的個數(shù)

len()內(nèi)置函數(shù)

str3 = 'project'
print(len(str3), len('abc 123\n'))

補充:空串

str4 = ''
str5 = ""
print(len(str5))

len1 = len(str3) # len1 = 7
index = len1 - 1 # 6
print(str3[-1], str3[index])

6.阻止轉(zhuǎn)義

在字符串的最前面添加r/R可以阻止轉(zhuǎn)義

print('a\nb','a\nb\')
print(len('a\nb\')) # 4

print(r'a\nb',R'a\nb\')
print(len(r'a\nb\')) # 6

print('a\nb\\') # a\nb\
print('\\n\') # \換行\(zhòng)

print(r'\\n\') # \\n\

練習(xí)

str1 = r'\nabc123' 求:str1[3] b

str2 = 'abc123\123' 求: str2[-5] 3

python為字符串提供了很多的內(nèi)建函數(shù)

字符串.函數(shù)()

注意:這些所有的函數(shù)的功能都不會影響原來的字符串,而是產(chǎn)生一個新的字符串

str1 = 'hello python'

1.capitalize() 將字符串的第一個字符轉(zhuǎn)換為大寫

newstr = str1.capitalize()
print(newstr, str1)

print('abc'.capitalize())

2.center(width, fillchar)

讓字符串變成width對應(yīng)的長度,原內(nèi)容居中,剩余的部分使用fillchar的值來填充

witdh - 整數(shù); fillchar - 任意字符

print('abc'.center(10, '*')) # ***abc****

3. rjust(width, fillchar)

讓字符串變成width對應(yīng)的長度,原內(nèi)容靠右,剩余的部分使用fillchar的值來填充

2015103001 2015103002 2015103015

1 2 3 4 5 11 20 ----> 001 002 003 010 011 012

1 --> 0001 11 -> 0011 123 -> 0123

number = '12'
new_id = number.rjust(4, '0')
print(new_id)

3.原字符串.count(str)

判斷str值在原字符串中出現(xiàn)的次數(shù)

str -> 字符串

print('abcabaa'.count('ab'))

print('貳1壹二2一'.isnumeric())

4.str1.join(str2)

在str2中的每個字符串之間插入一個str1

print('-+'.join('abc'))

5.str1.replace(old, new)

將str1中old全部替換成new

new_str = 'abcdahulapuyeahj'.replace('a','+')
print(new_str)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容