掌握字符串內建函數

字符串內建函數

字符串作爲常用的一種數據類型,它提供了很多內建函數,接下來,列舉一些字符串常用的函數。

1,find 函數

find 函數用於檢測字符串中是否包含子字符串 sub,如果指定 start(開始)和 end (結束)範圍,則檢查是否包含在指定範圍內。如果包含子字符串則返回開始的索引值,否則返回 -1。

語法格式如下:

str.find(sub[, start[, end]])

參數含義如下:
(1) sub:指定索引的字符串
(2)start:開始索引,默認爲0
(3)end:結束索引,默認爲字符串的長度

例如:查找 “itheima”是否在字符串 sting_example中,代如下:

string_example = 'hello word itheima and itheimaApp'
index = string_example.find("itheima")
print(index)

運行結果如下:

在這裏插入圖片描述

2, index 函數

index 函數檢測字符串中是否包含子字符串 sub , 如果指定start(開始)和 end (結束)範圍,則檢查是否包含在指定的範圍內。如果包含子字符串,則返回子字符串開始的索引值,否則拋出異常。

語法格式如下:

str.index(sub[, start[, end]])

參數含義如下:
(1) sub:指定檢索的字符串
(2)start:開始索引,默認爲0
(3)end:結束索引,默認爲字符串的長度

通過下列案例演示 index 函數的使用:

string_example = 'hello word itheima and itheimaApp'
index = string_example.index("itheima", 0, 10)
print(index)

運行結果如下:(子字符串不在索引範圍, 因此程序報錯)
在這裏插入圖片描述

3,count 函數

count函數用於統計字符串 sub 字串出現的次數,可以設定開始與結束位置來限制字符串的搜索範圍,該函數返回子字符串在字符串中出現的次數。

語法格式如下:

str.count(sub[, start[, end]])

參數含義如下:
(1)sub:搜索的子字符串
(2)start:字符串開始搜索的位置。默認爲第一個字符,該字符串引值爲0
(3)end:字符串結束搜索的位置。默認爲字符串的長度

通過下列案例演示,代碼如下:

string_example = 'hello word itheima and itheimaApp'
result = string_example.count("itheima")
print(result)

運行結果如下:
在這裏插入圖片描述

4,replace 函數

replace 函數把字符串中 old(舊字符串)替換成 new(新字符串),該函數返回的是字符串中old (舊字符串)替換成 new(新字符串)後生成的新字符串。如果指定第3個參數count,則替換不超過count 次。

語法格式如下:

str.replace(old, new[, count])

參數含義如下:
(1)old:將被替換的子字符串
(2)new:新字符串,用於替換 old 子字符串
(3)count:可選參數,替換不超過 count 次

通過下列案例演示 replace函數的使用:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.replace('itheima', 'ITHEIMA', 1)
print(new_string)

運行結果如下:
在這裏插入圖片描述

5,split 函數

split 函數通過指定分隔符對字符串進行切片,如果參數 maxsplit 有指定值,則僅分割 maxsplit 個字符串,該函數的返回值是分割後的字符串列表。

語法格式如下:

str.split(sep = None, maxsplit = -1)

參數含義如下:
(1)sep:分隔符,默認爲所有空字符串,包括空格、換行(\n)、製表符(\t)等
(2)maxsplit:分割次數。

通過下列案例演示 split 函數的使用,代碼如下:

string_example = 'this is string example ....wow!!!'
print(string_example.split())
print(string_example.split('i', 1))
print(string_example.split('w'))

運行結果如下:
在這裏插入圖片描述

6,capitalize 函數

capitalize 函數用於將字符串的第一個字母變成大寫,其他字母變成小寫,該函數返回一個首字母大寫的字符串。

語法格式如下:

str.capitalize()

通過下列案例演示 capitalize 函數的使用,代碼如下:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.capitalize()
print(new_string)

運行結果如下:
小脆筒style

7,title 函數

title 函數返回 “標題化” 的字符串,也就是說所有的單詞都是以大寫開始,其餘字母均爲小寫。

語法格式如下:

str.title()

通過下列案例演示,代碼如下:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.title()
print(new_string)

運行結果如下:
在這裏插入圖片描述

8,startswith 函數

startswith 函數用於檢查字符串是否是以指定子字符串開頭,如果是,則返回True,否則返回False. 若指定了start和end 參數的值,則會在指定的範圍內檢查。

語法格式如下:

str.startswith(prefix[, start[, end]])

參數含義如下:
(1) prefix:檢測的字符串
(2)start:可選參數,用於設置字符串檢測的起始位置
(3)end:可選參數,用於設置字符串檢測的結束位置

通過下列案例演示startswith 函數的使用,代碼如下:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.startswith('hello')
print(new_string)

運行結果如下:
在這裏插入圖片描述

9,endswith 函數

endswith 函數用於判斷字符串是否以指定後綴結尾,如果以指定後綴結尾返回True,否則返回Flase。可選參數start 與end爲檢索字符串的開始與結束位置。

語法格式如下:

str.endswith(suffix[, start[, end]])

參數含義如下:
(1)suffix:檢測的字符串
(2)start:字符串中的開始位置
(3)end:字符串的結束位置

通過下列案例演示 endswith 函數的使用,代碼如下:

string_example = 'hello word itheima and itheimaApp'
new_string_one = string_example.endswith('app')
new_string_two = string_example.endswith('App')

print(new_string_one)
print(new_string_two)

運行結果如下:

在這裏插入圖片描述

10,upper 函數

upper 函數將字符串中的小寫字母轉換爲大寫字母,返回小寫字母轉換爲大寫字母的字符串。

語法格式如下:

str.upper()

通過下列案例演示,代碼如下:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.upper()
print(new_string)

運行結果如下:
在這裏插入圖片描述

11,ljust 函數

ljust 函數返回一個原字符串左對齊,並使用空格填充至指定長度的新字符串。如果指定的長度小於原字符串的長度,則返回原字符串。

語法格式如下:

str.ljust(width[, fillchar])

參數含義如下:
(1)width:指定字符串長度
(2)fillchar:填充字符,默認爲空格

通過下列案例演示,代碼如下:

old_string = 'hello word itheima and itheimaApp'
print(old_string.ljust(50, '*'))

運行結果如下:
在這裏插入圖片描述

12,rjust 函數

rjust 函數返回一個原字符串右對齊,並使用空格填充至長度爲 width 的新字符串。如果指定的長度小於原字符串的長度,則返回原字符串。

語法格式如下:

str.rjust(width[, fillchar])

參數含義如下:
(1)width:填充指定字符串後字符串的總長度
(2)fillchar:填充的字符,默認爲空格

通過下列案例演示 rjust 函數的使用,代碼如下:

old_string = 'hello word itheima and itheimaApp'
new_string = old_string.rjust(50)
print(new_string)

運行結果如下:

在這裏插入圖片描述

13,center 函數

center 函數返回一個寬度爲 width、原字符串居中、以 fillchar(默認爲空格)填充左右兩邊的字符串。

語法格式如下:

str.center(width[, fillchar])

參數含義如下:
(1)width:字符串的總長度
(2)fillchar:填充字符

14,lstrip 函數

lstrip 函數用於截掉字符串左邊的空格或指定字符,返回的是一個新字符串。

語法格式如下:

str.lstrip( [chars] )

參數含義如下:

chars:指定截取的字符

通過下列案例演示 lstrip 函數的使用,代碼如下:

old_string_one = 'hello word itheima and itheimaApp'
old_string_two = '       hello word itheima and itheimaApp  '
new_string_one = old_string_one.lstrip()
new_string_two = old_string_two.lstrip()
print(new_string_one)
print(new_string_two)

運行結果如下:
在這裏插入圖片描述

15,rstrip 函數

rstrip 函數用於刪除字符串末尾的指定字符,返回的是一個新的字符串。

語法格式如下:

str.rstrip([chars])

參數含義如下:
chars:指定刪除的字符串,默認爲空格。

16,strip 函數

strip 函數用於移除字符串頭尾指定的字符,返回的是一個新字符串。

語法格式如下:

str.strip([chars])

參數含義如下:
chars:指定刪除的字符串,默認爲空格。

通過下列案列演示,代碼如下:

old_string = '      hello word itheima and itheimaApp'
new_string = old_string.strip()
print(new_string)

運行結果如下:
在這裏插入圖片描述

補充: Unicode 字符串
1)在Python2 中,普通字符串是以8位 ASCII碼進行儲存的,而Unicode 子字符串中的字符則按照 Unicode 儲存,這樣能夠表示更多的字符集。
2)在Python3 中,所有的字符串都是Unicode 字符串。

END
感謝看到這裏的各位讀者朋友們,如果你感到本文寫的不錯,就順手點個贊收藏一下,也可以關注一波~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章