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

歡迎訪問 生活随笔!

生活随笔

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

python

Python 字符串 String 内建函数大全(1)

發布時間:2023/12/20 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 字符串 String 内建函数大全(1) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關于 Python 的字符串處理相關的方法還是非常多的,由于我正在學習 Python,于是就把 Python 中這些混雜的用于 string 的函數總結出來,在自己忘記的時候便于查找,希望對于有類似需求的人有所幫助。

captalize() 函數

功能

將一個字符串的第一個字母大寫

用法

str.captalize()

參數

返回值

string

示例代碼

str = "hello world!"print "str.capitalize(): ", str.capitalize()

運行結果

tr.capitalize(): Hello world!

center(width, fillchar) 函數

將字符串居中,居中后的長度為?width

功能

將字符串居中,居中后的長度為?width

用法

str.center(width[, fillchar])

參數

  • width:?表示字符串總長度
  • fillchar:?使字符串居中所填充的字符,默認為空格

返回值

返回填充字符后的字符串

示例代碼

str = "hello world!"print "str.center(20): ", str.center(20) print "str.center(20,'-'): ", str.center(20,'-')

運行結果

tr.center(20): hello world! str.center(20,'-'): ----hello world!----

本人對于Python學習創建了一個小小的學習圈子,為各位提供了一個平臺,大家一起來討論學習Python。歡迎各位到來Python學習群:923414804一起討論視頻分享學習。Python是未來的發展方向,正在挑戰我們的分析能力及對世界的認知方式,因此,我們與時俱進,迎接變化,并不斷的成長,掌握Python核心技術,才是掌握真正的價值所在。

count(str, start=0, end=len(string)) 函數

功能

返回該字符串中出現某字符串序列(或字符)的次數

用法

str.count(sub, start=0, end=len(string))

參數

  • sub:?被查找的字符串序列
  • start:?開始查找的索引位置,默認為字符串開始
  • end:?結束查找的索引位置,默認為字符串結束

返回值

被查找的序列在字符串的查找位置中出現的次數

示例代碼

str = "hello world! hello world!"sub = "o" print "str.count(sub): ", str.count(sub)sub = "hello" print "str.count(sub, 5) ", str.count(sub, 5)

運行結果

str.count(sub): 4 str.count(sub, 5) 1

decode(encoding=’UTF-8’,errors=’strict’) 函數 & encode(encoding=’UTF-8’,errors=’strict’)

功能

使用特定編碼將字符串解碼(decode)/編碼(encode)

用法

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

參數

  • encoding:?使用的編碼格式
  • errors:?設置不同的錯誤處理方法,其他選項有?ignore,?replace,?xmlcharrefreplace,?backslashreplace

返回值

編碼/解碼后的字符串

示例代碼

str = "hello world!"str = str.encode('base64', 'strict') print "Encoded str: ", str print "Decoded str: ", str.decode('base64')

運行結果

Encoded str: aGVsbG8gd29ybGQhDecoded str: hello world!

endswith(suffix, start=0, end=len(string)) 函數

功能

判斷字符串是否是以某字符串結尾的

用法

str.endswith(suffix, start=0, end=len(string))

參數

  • suffix:?被查找的字符串
  • start:?字符串查找的起始位置,默認為字符串起始位置
  • end:?字符串查找的結束位置,默認為字符串結束位置

返回值

如果字符串是以?suffix?結尾的返回?True, 否則返回?False

示例代碼

str = "hello world!"suffix = "world!" print str.endswith(suffix)suffix = "llo" print str.endswith(suffix,0,4) print str.endswith(suffix,0,5)

運行結果

True False True

expandstabs(tabsize=8) 函數

功能

提供自定義 tab(/t) 長度的方法,默認為8

用法

str.expandtabs(tabsize=8)

參數

  • tabsize:?表示自定義 tab 的長度

返回值

string

示例代碼

str = "hello\tworld!"print "Original str: " + str print "Defalut expanded tab: " + str.expandtabs(); print "Double expanded tab: " + str.expandtabs(16)

運行結果

Original str: hello world! Defalut expanded tab: hello world! Double expanded tab: hello world!

find(str, start=0, end=len(string)) 函數

功能

在字符串的某指定位置查找某字符串

用法

str.find(str, start=0, end=len(string))

參數

  • str:?被查找的子字符串
  • start:?查找的起始位置,默認為字符串起始位置
  • end:?查找的結束位置,默認為字符串結束位置

返回值

如果查找到,返回該子字符串的索引;未查找到,返回-1

示例代碼

str = "hello world!"str1 = "wo"print str.find(str1) print str.find(str1, 8)

運行結果

6 -1

index(str, start=0, end=len(string)) 函數

功能

功能上與 find() 相同,只是在未找到子字符串是拋出異常

用法

str.index(str, start=0, end=len(string))

參數

同 find()

返回值

如果查找到,返回該子字符串的索引;未查找到,拋出異常

示例代碼

str = "hello world!"str1 = "wo"print str.index(str1) print str.index(str1, 8)

運行結果

6 Traceback (most recent call last):File "teststrmethods.py", line 6, in <module>print str.index(str1, 8) ValueError: substring not found

isalnum() 函數

功能

判斷該字符串是否只是字母數字組合

用法

str.isalnum()

參數

返回值

如果該字符串是字母數字組合,返回?True,否則返回?False

示例代碼

str = "helloworld" print str.isalnum()str = "hello world" print str.isalnum()str = "hello123" print str.isalnum()str = "hello123!" print str.isalnum()

運行結果

True False True False

isalpha() 函數

功能

判斷該字符串是否是字母組合

用法

str.isalpha()

參數

返回值

如果該字符串是字母組合,返回?True,否則返回?False

示例代碼

str = "helloworld" print str.isalpha()str = "hello world" print str.isalpha()str = "hello123" print str.isalpha()str = "hello123!" print str.isalpha()

運行結果

True False False False

isdigit() 函數

功能

判斷該字符串是否只包含數字

用法

str.isdigit()

參數

返回值

如果該字符串只包含數字,則返回?True,否則返回?False

示例代碼

str = "hello123" print str.isdigit()str = "123456" print str.isdigit()

運行結果

False True

islower() 函數

功能

判斷該字符串中是否只是小寫字母

用法

str.islower()

參數

返回值

如果該字符串中只是小寫字母,返回True,否則返回False

示例代碼

str = "hello wolrd!" print str.islower()str = "Hello Wolrd!" print str.islower()

運行結果

True False

e() 函數

功能

判斷該字符串是否只包含空格

用法

str.isspace()

參數

返回值

如果該字符串只包含空格,返回True,否則返回False

示例代碼

str = " " print str.isspace()str = "Hello Wolrd!" print str.isspace()

運行結果

True False

istitle() 函數

功能

檢查該字符串中的單詞是否首字母都大寫

用法

str.istitle()

參數

返回值

如果該字符串中的單詞首字母都大寫了,返回True,否則返回False

示例代碼

str = "Hello world!" print str.istitle()str = "Hello Wolrd!" print str.istitle()

運行結果

False True

isupper() 函數

功能

判斷該字符串中的字母是否都是大寫

用法

str.isupper()

參數

返回值

如果該字符串中的字母都是大寫,返回True,否則返回False

示例代碼

str = "Hello world!" print str.isupper()str = "HELLO WORLD!" print str.isupper()

運行結果

False True

join(seq) 函數

功能

用該字符串連接某字符序列(seq)

用法

str.join(sequence)

參數

  • sequence:?被連接的字符序列

返回值

返回連接之后的字符串

示例代碼

str = "-" sequence = ("hello", "world", "everyone", "!")print str.join(sequence)

運行結果

hello-world-everyone-!

len(string) 函數

功能

得到該字符串的長度

用法

len(str)

參數

返回值

返回該字符串長度

示例代碼

str = "Hello world!"print "the length of str: ", len(str)

運行結果

the length of str: 12

ljust(width, fillchar=’ ‘)函數

功能

在字符串的右邊填充字符使得字符串達到指定長度

用法

str.ljust(width, fillchar=' ')

參數

  • width:?填充后的目標長度
  • fillchar:?用于填充的字符,默認為空格

返回值

返回填充后的字符串

示例代碼

str = "Hello world"print str.ljust(15) print str.ljust(15,'!')

運行結果

Hello world Hello world!!!!

轉載于:https://www.cnblogs.com/paisenpython/p/10276476.html

總結

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

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