1. 程式人生 > >Python String Methods

Python String Methods

capi pan span sta string 第一個 cnblogs div 居中

str.capitalize()  # 將字符串的第一個字母變成大寫,其他字母變小寫。對於 8 位字節編碼需要根據本地環境
>>> hello.capitalize()
Hello
>>> hEllo.capitalize()
Hello
string.center(width[, fillchar]) # 返回一個原字符串居中,並使用空格填充至長度 width 的新字符串。默認填充字符為空格
>>> hello.center(10,0)
00hello000
>>> hello.center(10)
hello
string.count(sub, start= 0,end=len(string)) #方法用於統計字符串裏某個字符出現的次數。可選參數為在字符串搜索的開始與結束位置
>>> hello world.count(o)
2
>>> hello world.count(o,4,8)
2
>>> hello world.count(o,4,7)
1

Python String Methods