日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python字符串内建函数详解

發布時間:2025/3/21 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python字符串内建函数详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概述

字符串方法是從python1.6到2.0慢慢加進來的——它們也被加到了Jython中。

這些方法實現了string模塊的大部分方法,如下表所示列出了目前字符串內建支持的方法,所有的方法都包含了對Unicode的支持,有一些甚至是專門用于Unicode的。


Python capitalize()方法

描述

Python capitalize()將字符串的第一個字母變成大寫,其他字母變小寫。對于 8 位字節編碼需要根據本地環境。

語法

capitalize()方法語法: str.capitalize()

參數

無。

返回值

該方法返回一個首字母大寫的字符串。

實例
以下實例展示了capitalize()方法的實例:

#!/usr/bin/pythonstr = "this is string example....wow!!!";print "str.capitalize() : ", str.capitalize()

以上實例輸出結果如下:

str.capitalize() : This is string example....wow!!!

Python center()方法

描述

Python center() 返回一個原字符串居中,并使用空格填充至長度 width 的新字符串。默認填充字符為空格。

語法

center()方法語法: str.center(width[, fillchar])

參數

width – 字符串的總寬度。 fillchar – 填充字符。

返回值

該方法返回一個原字符串居中,并使用空格填充至長度 width 的新字符串。

實例
以下實例展示了center()方法的實例:

#!/usr/bin/pythonstr = "this is string example....wow!!!";print "str.center(40, 'a') : ", str.center(40, 'a')

以上實例輸出結果如下:

str.center(40, 'a') : aaaathis is string example....wow!!!aaaa

Python count()方法

描述

Python count() 方法用于統計字符串里某個字符出現的次數。可選參數為在字符串搜索的開始與結束位置。

語法

count()方法語法: str.count(sub, start= 0,end=len(string))

參數

sub – 搜索的子字符串
start – 字符串開始搜索的位置。默認為第一個字符,第一個字符索引值為0。
end –字符串中結束搜索的位置。字符中第一個字符的索引為 0。默認為字符串的最后一個位置。

返回值

該方法返回子字符串在字符串中出現的次數。

實例
以下實例展示了count()方法的實例:

#!/usr/bin/pythonstr = "this is string example....wow!!!";sub = "i"; print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40) sub = "wow"; print "str.count(sub) : ", str.count(sub)

以上實例輸出結果如下:

str.count(sub, 4, 40) : 2 str.count(sub, 4, 40) : 1

Python decode()方法

描述

Python decode() 方法以 encoding 指定的編碼格式解碼字符串。默認編碼為字符串編碼。

語法

decode()方法語法:
str.decode(encoding=’UTF-8’,errors=’strict’)

參數

encoding – 要使用的編碼,如”UTF-8”。
errors – 設置不同錯誤的處理方案。默認為
‘strict’,意為編碼錯誤引起一個UnicodeError。 其他可能得值有 ‘ignore’, ‘replace’,’xmlcharrefreplace’, ‘backslashreplace’ 以及通過 codecs.register_error() 注冊的任何值。

返回值

該方法返回解碼后的字符串。

實例
以下實例展示了decode()方法的實例:

#!/usr/bin/pythonstr = "this is string example....wow!!!"; str = str.encode('base64','strict');print "Encoded String: " + str; print "Decoded String: " + str.decode('base64','strict')

以上實例輸出結果如下:

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=Decoded String: this is string example....wow!!!

Python encode()方法

描述

Python encode() 方法以 encoding 指定的編碼格式編碼字符串。errors參數可以指定不同的錯誤處理方案。

語法

str.encode(encoding=’UTF-8’,errors=’strict’)

參數

encoding – 要使用的編碼,如”UTF-8”。

errors – 設置不同錯誤的處理方案。默認為 ‘strict’,意為編碼錯誤引起一個UnicodeError。 其他可能得值有’ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ 以及通過 codecs.register_error() 注冊的任何值。

返回值

該方法返回編碼后的字符串。

實例
以下實例展示了encode()方法的實例:

#!/usr/bin/pythonstr = "this is string example....wow!!!";print "Encoded String: " + str.encode('base64','strict')

以上實例輸出結果如下:

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=

Python endswith()方法

描述

Python endswith()方法用于判斷字符串是否以指定后綴結尾,如果以指定后綴結尾返回True,否則返回False。

可選參數”start”與”end”為檢索字符串的開始與結束位置。

語法

endswith()方法語法: str.endswith(suffix[, start[, end]])

參數

suffix – 該參數可以是一個字符串或者是一個元素。
start – 字符串中的開始位置。
end – 字符中結束位置。

返回值

如果字符串含有指定的后綴返回True,否則返回False。

實例
以下實例展示了endswith()方法的實例:

#!/usr/bin/pythonstr = "this is string example....wow!!!";suffix = "wow!!!"; print str.endswith(suffix); print str.endswith(suffix,20);suffix = "is"; print str.endswith(suffix, 2, 4); print str.endswith(suffix, 2, 6);

以上實例輸出結果如下:

True True True False

Python expandtabs()方法

描述

Python expandtabs() 方法把字符串中的 tab 符號(‘\t’)轉為空格,tab 符號(‘\t’)默認的空格數是 8。

語法

expandtabs()方法語法: str.expandtabs(tabsize=8)

參數

tabsize – 指定轉換字符串中的 tab 符號(‘\t’)轉為空格的字符數

返回值

該方法返回字符串中的 tab 符號(‘\t’)轉為空格后生成的新字符串。

實例
以下實例展示了expandtabs()方法的實例:

#!/usr/bin/pythonstr = "this is\tstring example....wow!!!";print "Original string: " + str; print "Defualt exapanded tab: " + str.expandtabs(); print "Double exapanded tab: " + str.expandtabs(16);

以上實例輸出結果如下:

Original string: this is string example....wow!!! Defualt exapanded tab: this is string example....wow!!! Double exapanded tab: this is string example....wow!!!

Python find()方法

描述

Python find() 方法檢測字符串中是否包含子字符串 str ,如果指定 beg(開始) 和 end(結束) 范圍,則檢查是否包含在指定范圍內,如果包含子字符串返回開始的索引值,否則返回-1。

語法

find()方法語法: str.find(str, beg=0, end=len(string))

參數

str – 指定檢索的字符串
beg – 開始索引,默認為0。
end – 結束索引,默認為字符串的長度。

返回值

如果包含子字符串返回開始的索引值,否則返回-1。

實例
以下實例展示了find()方法的實例:

#!/usr/bin/pythonstr1 = "this is string example....wow!!!"; str2 = "exam";print str1.find(str2); print str1.find(str2, 10); print str1.find(str2, 40);

以上實例輸出結果如下:

15 15 -1

Python index()方法

描述

Python index() 方法檢測字符串中是否包含子字符串 str ,

如果指定 beg(開始) 和 end(結束)范圍,則檢查是否包含在指定范圍內

該方法與 python find()方法一樣,只不過如果str不在 string中會報一個異常。

語法

index()方法語法: str.index(str, beg=0, end=len(string))

參數

str – 指定檢索的字符串
beg – 開始索引,默認為0。
end – 結束索引,默認為字符串的長度。

返回值

如果包含子字符串返回開始的索引值,否則拋出異常。

實例
以下實例展示了index()方法的實例:

#!/usr/bin/pythonstr1 = "this is string example....wow!!!"; str2 = "exam";print str1.index(str2); print str1.index(str2, 10); print str1.index(str2, 40);

以上實例輸出結果如下:

15 15 Traceback (most recent call last):File "test.py", line 8, in print str1.index(str2, 40); ValueError: substring not foundshell returned 1

異常處理的部分后續再說~



其他

總結

以上是生活随笔為你收集整理的python字符串内建函数详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。