Python學(xué)習(xí)筆記三:字符串處理

字符串

1.字符串的本質(zhì)是字符序列

2.字符串是不可變的


'''

3.創(chuàng)建字符串

單行數(shù)據(jù)

a.單引號(hào)創(chuàng)建 'String'

b.雙引號(hào)創(chuàng)建 " String"

使用兩種方式創(chuàng)建的原因, 可以創(chuàng)建本身就包含引號(hào)的字符串而不使用轉(zhuǎn)義符

多行數(shù)據(jù)

a.三個(gè)單引號(hào)\'''

b.三個(gè)雙引號(hào)"""String"""

'''

#test1 測(cè)試打印多行字符串

abc='''There was a Young Lady of Norway,

who casually sat in a doorway;

when the door squeezed her flat,

She exclaimed,"What of that?"

This courageous Young Lady of Norway'''

print(abc)

'''

4.python 允許空串的存在

'''

#test2 測(cè)試python允許空串的存在

a1=''

print(a1)

'''

5.類型轉(zhuǎn)換 str()函數(shù)

str(Object)? 將任意類型的數(shù)據(jù)轉(zhuǎn)換為字符串類型

'''

print(str(123))

print(str(33.22))

print(str(True))

'''

6.轉(zhuǎn)義\

\n? 換行

\t? 制表符

'''

print("abc\nbcd")

print("abc\tbcd")

print("abc\\cd")

'''

7.字符串拼接

1.使用+? ? "abc"+"bcd"

2.直接拼接 "abc""bcd"

3.使用print()方法進(jìn)行拼接 print(a,b,c)? 結(jié)果為a b c 會(huì)為每個(gè)拼接字符間自動(dòng)添加空格

'''

#test3 測(cè)試字符串拼接

m="abc"

n="bcd"

print(m+n)

print("abc""bcd")

print(m,n)

'''

8.復(fù)制字符串 *

字符串*數(shù)量 可以復(fù)制多個(gè)字符串

'''

#test4 測(cè)試復(fù)制字符串

print("測(cè)試"*4)

'''

9.提取字符串? []

1.在字符串名后面添加[]? []內(nèi)添加偏移量可以提取該位置的單個(gè)字符

偏移量最左的是0 ,最右的是-1

2.偏移量超過(guò)字符串的長(zhǎng)度 異常提醒 越界異常 IndexError:string index out of range

'''

#test5 測(cè)試字符串提取

teststr="abcdefghijklmnopqrstuvwxyz"

print(teststr[0])

print(teststr[-1])

print(teststr[1])

'''

10.改變字符串內(nèi)容 replace()

replace(old, new[, count])

old -將被替換的字符

new -替換成的字符

count -替換最多不超過(guò)多少次

'''

#test6 測(cè)試替換字符串

testold="abcabcabcabc"

testnew=testold.replace("a","b",2)

'''

11.使用[start:end:step]切片

start? --起始偏移量

end? ? --終止偏移量

step? --步長(zhǎng)

a.[:]? 提取從頭到尾的整個(gè)字符串

b.[start:]? 從start提取到結(jié)尾

c.[:end]? 從開頭截取到end-1

python的提取操作不包含最后一個(gè)偏移量,所以分片的end偏移量需要比實(shí)際提取的最后一個(gè)字符的偏移量多1

d.[start:end]? 從start截取到end-1

e.[start:end:step] 從start截取到end-1 每step個(gè)字符截取一個(gè)

ps:無(wú)效偏移量 若小于其實(shí)位置的偏移量會(huì)當(dāng)作0,大于終止位置的偏移量會(huì)被當(dāng)作-1

'''

#test7 測(cè)試切片[:]

testqp="abcabcabcabc"

print(testqp[:])

print(testqp[1:])

print(testqp[:-1])

print(testqp[1:-1])

print(testqp[0:-1:3])

'''

12.獲得字符串的長(zhǎng)度 len()

len(字符串)

'''

#test8 測(cè)試獲取字符串長(zhǎng)度

a=len(testqp)

print(a)

'''

13.使用split() 分割字符串

split(sep=None, maxsplit=-1)

sep 指定sep字符為分割符,若未指定,默認(rèn)使用空白字符(空格,換行,制表符)

maxsplit 最多分割多少次

返回值:列表

'''

#test9 測(cè)試分割函數(shù)

b="1,2,3,4,"

c="1 2 3 4"

print(b.split(","))

print(b.split(",",2))

print(b.split())

print(c.split())

'''

13.合并字符串 join()

str.join(sequence)

str? -粘合字符串間的分隔符

sequence --需要粘合的序列(列表)

'''

#測(cè)試合并字符串方法

d=["hello","word","haha"]

dnew=".".join(d)

print(dnew)

'''

14.常用字符串函數(shù)

'''

#定義所需要的字符串

poem='''All that doth flow we cannot liquid name

Or else would fire and water be the same;

But that is liquid which is moist and wet

Fire that property can never get.

Then 'tis not cold that doth the fire put out

But 'tis the wet that makes it die,no doubt.'''

#test1? 提取開頭的13個(gè)字符

print(poem[:13])

#test2? 計(jì)算這首詩(shī)有多少個(gè)字符

print(len(poem))

#test3? 判斷這首詩(shī)是不是以All開頭

print(poem.startswith("All"))

#test4? 這首詩(shī)是否以That’s all,folks!結(jié)尾

print(poem.endswith("That's all,folks!"))

#test5? 查找詩(shī)中第一次出現(xiàn)單詞the的位置(偏移量)

print(poem.find("the"))

#test6? 查找詩(shī)中最后一次出現(xiàn)單詞the的位置(偏移量)

print(poem.rfind("the"))

#test7? the在詩(shī)中一共出現(xiàn)多少次

print(poem.count("the"))

#test8? 詩(shī)中所有字符都是字母或者數(shù)字么?

print(poem.isalnum())

'''

15.大小寫以及對(duì)齊方式處理

'''

e="aBc www abc"

#test1 去除字符串頭尾指定字符

print(e.strip("acb"))

#test2 將字符串的首字母大寫

print(e.capitalize())

#test3 將字符串每個(gè)單詞的首字母大寫

print(e.title())

#test4 將字符串所有字符轉(zhuǎn)換為大寫

print(e.upper())

#test5 將字符串所有字符轉(zhuǎn)換為小寫

print(e.lower())

#test6 將字符串所有字符大小寫轉(zhuǎn)換

print(e.swapcase())

#test7 在30個(gè)字符位居中

print(e.center(30))

#test8 左對(duì)齊

print(e.ljust(30))

print(e.rjust(30))

'''

16.字符串替換 replace()

replace(a,b,c)

a? --需要被替換的字符串

b? --替換后的字符串

c? --最多替換多少次

'''

#test9 測(cè)試替換函數(shù)

print(e.replace("w","d",2))

print(e)

'''

練習(xí):

1.計(jì)算一個(gè)小時(shí)有多少秒賦值給seconds_per_hour

2.計(jì)算一天有多少秒賦值給seconds_per_day

3.用second_per_day除以second_per_hour 浮點(diǎn)數(shù)除法

4.用second_per_day除以second_per_hour 整數(shù)除法

'''

seconds_per_hour=60*60

seconds_per_day=24*seconds_per_hour

float_day_hour=seconds_per_day/seconds_per_hour

int_day_hour=seconds_per_day//seconds_per_hour

print(seconds_per_hour)

print(seconds_per_day)

print(float_day_hour)

print(int_day_hour)

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

相關(guān)閱讀更多精彩內(nèi)容

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