Python學習筆記 字串

2021-08-20 06:11:41 字數 3937 閱讀 8256

1--字串的定義

。--字串就是一串字元,是程式語言中表示文字的資料型別

。--在python中可以使用一堆雙引號""或者一對單引號''定義乙個字串

--雖然可以使用\"或者\'做字串的轉義,但是在實際開發中:

-如果字串內部需要使用",可以使用'定義字串

-如果字串內部需要使用',可以使用"定義字串

。--也可以使用索引獲取乙個字串中,指定位置的字元,索引計數從0開始

。--也可以使用for迴圈遍歷字串中的每乙個字元

格式:string="hello python"

for c in string:

print(c)

練習:str1="3464"

str1="i wish 'you' are here"

print(str1)---->3464

print(str1[2])---->6

for car in str1:

print(char)

2--字串的常用操作函式

。--在ipython3中定義乙個字串,例如:hello_str=""

。--輸入hello_str.按下tab鍵,會提示字串能夠使用的方法,其中

hello_str="hello hello"

#統計字串的長度

print(len(hello_str))

#統計某乙個小(子)字串出現的次數

print(hello_str.count("llo"))---->2

print(hello_str.count("123"))---->0

#某乙個子字串出現的位置--如果使用index方法傳遞的子字串不存在,程式會報錯

print(hello_str.index("llo"))---->2

print(hello_str.index("123"))---->報錯valueerror:substring not found--值錯誤:子字串沒有發現

--判斷型別

space_str="   \t\n\r"

#判斷字串中是否含有空白字元(\也算是空白字元)

print(space_str.isspace())---->true

num_str="12"

#判斷字串中是否只包含數字

#1>都不能判斷小數,否則會在控制台輸出false

print(num_str)

#判斷字串中是否都是數字,是,就返回ture,使用頻率較高

print(num_str.isdecimal())

#判斷字串中是否都是數字、unicode字串(比如\u00b2)

print(num_str.isdigit())

#判斷字串中是否都是數字、unicode字串(比如\u00b2)、中文數字,是,返回ture

print(num_str.isnumberic())

--替換和查詢

hello_str="hello world"

#判斷是否以指定字串開始(分字母大小寫)

print(hello_str.startswith("hello"))---->ture

print(hello_str.startswith("hello"))---->false

#判斷是否以指定字串結束

print(hello_str.endswith("hello"))----ture

#查詢指定字串

#某乙個子字串出現的位置--如果使用index方法傳遞的子字串不存在,程式會報錯

print(hello_str.find("llo"))---->2

print(hello_str.index("llo"))---->2

print(hello_str.find("123"))---->-1

#兩者的區別

#index如果指定的字串不存在,會報錯

#find如果指定的字串不存在,會返回-1

#替換字串--replace方法執行完之後,會返回乙個新的字串

#注意:不會修改原有字串的內容

print(hello_str.replace("world","python"))---->hello python

print(hello_str)---->hello world

--文字對齊

string="24543"

#原字串靠左邊對齊

string.ljust(距離左邊框寬度,空格(預設為英文空格,可以設定為中文空格))

#用法print("|%s|" %string.ljust(10," "))

#原字串靠右邊對齊

string.rjust(寬度,' ')  #為中文空格

#原字串居中對齊

string.center(寬度)

#預設為英文空格

--去除空白字元-strip(去掉)

string="24 5 43"

#截掉string左邊(開始)的空白字元

string.lstrip()

#用法print("|%s|" %string.lstrip())

#截掉string末尾(開始)的空白字元

string.rstrip()

#截掉string左右兩邊的空白字元

string.strip()

--拆分和連線

補充:乙個字串內部,可以包含多個子字串

例如:aa_str="12\t23 2454\n8923\r"

string="24 5 43"

#以str為分隔符拆分string,如果num有指定值,則僅分隔num+1個子字串,str若不指定,則str預設包含\t,\r,\n和空格

new_str=string.split(str="",num)

#以seq作為分隔符,將string中所有的元素(的字串表示)合併為乙個新的字串

new_str=seq.join(string)

例:new_str=" ".join(string)

3--字串的切片

。--切片方法適用於字串、列表、元組

--切片使用索引值來限定範圍,從乙個大的字串中,切出小的字串

--列表和元組都是有序的集合,都能夠通過索引值獲取到對應的資料

--字典是乙個無序的集合,是使用鍵值對儲存資料

格式:字串[開始索引:結束索引:步長]

**注意:

--指定的區間屬於 左閉右開 型,[開始索引,結束索引) => 開始索引 >= 範圍 < 結束索引

-從起始位開始,到結束位前一位結束(不包含結束位本身)

--從頭開始,開始索引數字可以省略,冒號不能省略

--到末尾結束,結束索引數字可以省略,冒號不能省略

--步長預設為1,如果連續切片,數字和冒號都可以省略

**補充:字串索引

正序從零開始依次遞增

逆序從-1,-2依次遞減

num_str="0123456789"

#擷取從2-5位置的字串

nm_str[2:6]

#擷取從2-末尾的字串

num_str[2:]

#擷取從開始-5的位置的字串

num_str[:6]

#擷取完整的字串

num_str[:]

#從開始位置,每個乙個字元擷取字串

num_str[::2]

#從索引1開始,每隔乙個取乙個

num_str[1::2]

#擷取從2-(末尾-1)的字串

num_str[2:-1]

#擷取字串末尾兩個字元

num_str[-2:]

#字串的逆序(面試題)

num_str[-1::-1]---->'9876543210'

num_str[::-1]---->'9876543210'

特殊:num_str[-1]---->'9'

num_str[0::-1]---->'0'

Python 學習筆記 字串

今天學習python對字串的一些基礎處理,感覺對於工作中的自動化指令碼傳送cli命令會很有幫助。首先最重要的是 標稱 轉換說明符 conversion specifier 用於字串格式化。左側放置乙個字串 格式化字串 而右側放置希望被格式化的值 待格式化的值 1 2 3 4 5 left hello...

Python學習筆記 字串

單引號 引用字元 雙引號 引用字串 三個單引號或者三個雙引號 引用多行字串 字串中包含單引號或雙引號 用轉義符 轉移符後面的字元表示字元本意 在字串中包含雙引號,則用單引號引用 print 這裡有個雙引號 在字串中包含單引號,則用雙引號引用 print 這裡有個單引號 即希望包含單引號,又希望包含雙...

Python學習筆記 字串

由0個或多個字元的集合 字串 或 常規字串跨多行,只要在行尾上加上 反斜槓 當字串內有干擾時可用 反斜槓進行轉義 長字串 用三個連續的雙引號或單引號來包很長的字串,可在中間換行ptint to be,or not to be that is the question 原始字串 其內的字串不會對反斜槓...