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!----

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:  aGVsbG8gd29ybGQh

Decoded 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

isspace() 函數

功能

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

用法

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!!!!

本文的版權歸作者 羅遠航 所有,採用 Attribution-NonCommercial 3.0 License。任何人可以進行轉載、分享,但不可在未經允許的情況下用於商業用途;轉載請註明出處。感謝配合!

發佈了105 篇原創文章 · 獲贊 237 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章