用法:
bytes.title()
bytearray.title()
返回二進製序列的標題版本,其中單詞以大寫 ASCII 字符開頭,其餘字符為小寫。未大小寫的字節值保持不變。
例如:
>>> b'Hello world'.title() b'Hello World'
小寫 ASCII 字符是序列
b'abcdefghijklmnopqrstuvwxyz'
中的那些字節值。大寫 ASCII 字符是序列b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
中的那些字節值。所有其他字節值都是不區分大小寫的。該算法使用一個簡單的獨立於語言的單詞定義作為一組連續的字母。該定義在許多情況下都有效,但這意味著縮寫和所有格中的撇號形成單詞邊界,這可能不是預期的結果:
>>> b"they're bill's friends from the UK".title() b"They'Re Bill'S Friends From The Uk"
可以使用正則表達式構造撇號的解決方法:
>>> import re >>> def titlecase(s): ... return re.sub(rb"[A-Za-z]+('[A-Za-z]+)?", ... lambda mo: mo.group(0)[0:1].upper() + ... mo.group(0)[1:].lower(), ... s) ... >>> titlecase(b"they're bill's friends.") b"They're Bill's Friends."
注意
此方法的 bytearray 版本確實
not
就地操作 - 它總是產生一個新對象,即使沒有進行任何更改。
相關用法
- Python bytes.isupper用法及代碼示例
- Python bytes.zfill用法及代碼示例
- Python bytes.isalpha用法及代碼示例
- Python bytes.hex用法及代碼示例
- Python bytes.isalnum用法及代碼示例
- Python bytes.removesuffix用法及代碼示例
- Python bytes.split用法及代碼示例
- Python bytes.lstrip用法及代碼示例
- Python bytes.expandtabs用法及代碼示例
- Python bytes.splitlines用法及代碼示例
- Python bytes.rstrip用法及代碼示例
- Python bytes.isdigit用法及代碼示例
- Python bytes.istitle用法及代碼示例
- Python bytes.removeprefix用法及代碼示例
- Python bytes.strip用法及代碼示例
- Python bytes.islower用法及代碼示例
- Python bytes()用法及代碼示例
- Python bytearray()用法及代碼示例
- Python binascii.crc32用法及代碼示例
- Python base64.b64decode()用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 bytes.title。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。