Python筆記 字串

2021-10-14 10:13:14 字數 1574 閱讀 7971

# 字串是不可變資料型別!!!

a ='curry'

b ="kobe"

c ='''stephen'''

d ="""bryant"""

# \' 顯示乙個普通的單引號

# \" 顯示乙個普通的雙引號

# \n 換行

# \t 製表符,4個空格

# \\ 前面乙個\是對後面\的轉義,乙個普通的\

print

('hello wor\\ld'

)# 在字串前面新增乙個r/r,表示原生字串

p = r'hello \teacher'

print

(p)p =

'hello \teacher'

print

(p)# 字串是不可變的資料型別,對於字串的任何操作,都不會改變原有的字串

# 切片就是從字串裡複製一段指定的內容,生成乙個新的字串

# 切片語法:x[start:end:step] 包含開始,不包含結束 step為步長,即間隔 步長預設為1,步長不能為0

x ='123456789'

print

(x)print

(x[0:9

:1])

a ='abcdefghijklmnopkrstuvwxyz'

print

(a[0:25

])print

(a[0:]

)print

(a[:25]

)print

(a[25:2

:-1]

)print

(a[::]

)print

(a[::-

1])# 倒序

print

(a[-1:

-25])

# 從右往左取

print

(len

(a))

# 內建函式len可以獲取字串的長度

print

(a.find(

'z')

)# find,index,rfing,rindex可以獲取指定字元的下表

python =

'hello python'

python.replace(

'hello'

,'hi'

)new = python.replace(

'hello'

,'hi'

)print

(python)

print

(new)

# split rsplit splitlines partition rpartition 可以將乙個字串切割成乙個列表

x ='zhangsan-lisi-wangwu-jerry-tom-tony'

y = x.split(

'-')

print

(x)print

(y)print

(x.split(

'-',2)

)print

(x.rsplit(

'-',2)

)

python筆記 字串

最重要的 序列資料型別 字串 格式 單行字串,單引號或者雙引號包裹,如果必要,需要交叉使用 多行字串,三引號,或者轉義字串,有個點 三引號的字串也是可以賦值給變數的,同單引號雙引號包裹的字串一樣 基本方法贅述一下 string.capitalize 將字串的 首字母大寫 string.title 將...

Python筆記 字串

print hello world capitalize 讓第乙個單詞首字母大寫 print hello world upper 全大寫 print hello world lower 全小寫 print hello world title 每個單詞的首字母大寫 ljust,rjust ljust ...

python 字串 筆記

1 單引號 雙引號 三引號是等價的 2 轉義字元 將轉義字元後的符號 看做乙個字元 print i said,don t do it 3 三引號 可以輸入多行文字,在輸入結束三引號之前這些文字不會被處理。print hello i am here 4 換行 n 轉義字元 在多行中列印文字 更有效率且...