Python 字符串(center)


center

描述

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

語法

center()方法語法:

str.center(width[, fillchar])

參數

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

返回值

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

1 s="hELLo WORlD"
2 print(s.center(50,"%"))
1 %%%%%%%%%%%%%%%%%%%hELLo WORlD%%%%%%%%%%%%%%%%%%%%

count()

描述

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

語法

count()方法語法:

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

參數

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

返回值

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

1 s="hELLo WooRlD"
2 print(s.count("o",1,10)) 

endswith()

描述

Python endswith() 方法用於判斷字符串是否以指定后綴結尾,如果以指定后綴結尾返回True,否則返回False。可選參數"start"與"end"為檢索字符串的開始與結束位置。

語法

endswith()方法語法:

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

參數

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

返回值

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

expandtabs()

描述

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

語法

expandtabs()方法語法:

str.expandtabs(tabsize=8)

參數

  • tabsize -- 指定轉換字符串中的 tab 符號('\t')轉為空格的字符數。

返回值

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

1 str = "this is\t string example....wow!!!"
2 print("Original string: " + str)
3 print ("Defualt exapanded tab: " +  str.expandtabs())
4 print( "Double exapanded tab: " +  str.expandtabs(16)) #擴展Tab鍵

輸出結果:

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

find()

描述

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

語法

find()方法語法:

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

參數

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

返回值

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

 

 rfind()

描述

Python rfind() 返回字符串最后一次出現的位置(從右向左查詢),如果沒有匹配項則返回-1。

語法

rfind()方法語法:

str.rfind(str, beg=0 end=len(string))

參數

  • str -- 查找的字符串
  • beg -- 開始查找的位置,默認為 0
  • end -- 結束查找位置,默認為字符串的長度。

返回值

返回字符串最后一次出現的位置,如果沒有匹配項則返回-1。

 1 str = "this is really a string example....wow!!!"
 2 substr ="is"
 3 
 4 print(str.rfind(substr))
 5 print(str.rfind(substr, 0, 10))
 6 print(str.rfind(substr, 10, 0))
 7 print("*"*20)
 8 print(str.find(substr))
 9 print(str.find(substr, 0, 10))
10 print(str.find(substr, 10, 0))
1 5
2 5
3 -1
4 ********************
5 2
6 2
7 -1

isalnum()

描述

Python isalnum() 方法檢測字符串是否由字母和數字組成。

語法

isalnum()方法語法:

str.isalnum()

參數

  • 無。

返回值

如果 string 至少有一個字符並且所有字符都是字母或數字則返回 True,否則返回 False

1 str = "this2009"  # 字符中沒有空格
2 print(str.isalnum())
3 
4 str = "this is string example....wow!!!"
5 print(str.isalnum())
1 True
2 False

isalpha()

描述

Python isalpha() 方法檢測字符串是否只由字母組成。

語法

isalpha()方法語法:

str.isalpha()

參數

  • 無。

返回值

如果字符串至少有一個字符並且所有字符都是字母則返回 True,否則返回 False

 

isdecimal()

描述

Python isdecimal() 方法檢查字符串是否只包含十進制字符。這種方法只存在於unicode對象。

注意:定義一個十進制字符串,只需要在字符串前添加 'u' 前綴即可。

語法

isdecimal()方法語法:

str.isdecimal()

參數

返回值

如果字符串是否只包含十進制字符返回True,否則返回False。

 

 isdigit()

描述

Python isdigit() 方法檢測字符串是否只由數字組成。

語法

isdigit()方法語法:

str.isdigit()

參數

  • 無。

返回值

如果字符串只包含數字則返回 True 否則返回 False。

 

join()

描述

Python join() 方法用於將序列中的元素以指定的字符連接生成一個新的字符串。

語法

join()方法語法:

str.join(sequence)

參數

  • sequence -- 要連接的元素序列。

返回值

返回通過指定字符連接序列中元素后生成的新字符串。

1 name=["wudi","we"]
2 names=""
3 print(names.join(name))
1 wudiwe

 

http://www.runoob.com/python/python-strings.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM