1. 程式人生 > >Python 字串 String 內建函式大全(1)

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。任何人可以進行轉載、分享,但不可在未經允許的情況下用於商業用途;轉載請註明出處。感謝配合!