新聞中心
Python字符串由內(nèi)建的str類代表,那么str 類包含哪些方法呢?Python 非常方便,它甚至不需要用戶查詢文檔,Python 是“自帶文檔”的。

10年積累的做網(wǎng)站、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有公安免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
這里需要讀者簡單掌握兩個幫助函數(shù):
dir():列出指定類或模塊包含的全部內(nèi)容(包括函數(shù)、方法、類、變量等)。
help():查看某個函數(shù)或方法的幫助文檔。
例如,要查看 str 類包含的全部內(nèi)容,可以在交互式解釋器中輸入如下命令:
>>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
上面列出了 str 類提供的所有方法,其中以“_”開頭、“_”結(jié)尾的方法被約定成私有方法,不希望被外部直接調(diào)用。
如果希望查看某個方法的用法,則可使用 help() 函數(shù)。例如,在交互式解釋器中輸入如下命令:
>>> help(str.title) Help on method_descriptor: title(...) S.title() -> str Return a titlecased version of S, i.e. words start with title case characters, all remaining cased characters have lower case.
從上面介紹可以看出,str 類的 title() 方法的作用是將每個單詞的首字母大寫,其他字母保持不變。
在 str 類中與大小寫相關(guān)的常用方法如下:
title():將每個單詞的首字母改為大寫。
lower():將整個字符串改為小寫。
upper():將整個字符串改為大寫。
例如,如果希望看到 lower() 方法的相關(guān)用法,可運(yùn)行如下命令:
>>> help(str.lower) Help on method_descriptor: lower(...) S.lower() -> str Return a copy of the string S converted to lowercase.
如下代碼示范了 str 的與大小寫相關(guān)的常用方法:
a = 'our domain is crazyit.org' # 每個單詞首字母大寫 print(a.title()) # 字符串中所有字符都小寫 print(a.lower()) # 字符串中所有字符都大寫 print(a.upper())
運(yùn)行上面程序,可以看到如下輸出結(jié)果:
Our Domain Is Crazyit.Org our domain is crazyit.org OUR DOMAIN IS CRAZYIT.ORG
本文題目:創(chuàng)新互聯(lián)Python教程:Python字符串大小寫轉(zhuǎn)換的函數(shù)及用法
網(wǎng)站地址:http://m.5511xx.com/article/dhedjph.html


咨詢
建站咨詢
