Python 字串 istitle() 方法



Python 字串istitle() 方法用於檢查字串是否為標題大小寫字串。如果字串是標題大小寫字串且至少包含一個字元,則此方法返回 True。大寫字元只能跟在非大小寫字元之後,小寫字元只能跟在大小寫字元之後。如果遵循此規則,則此方法返回 True,否則返回 False。

讓我們在下一節中更詳細地瞭解此方法。

語法

以下是 Python 字串istitle() 方法的語法:

str.istitle()

引數

Python 字串istitle() 方法不包含任何引數。

返回值

如果字串中的所有單詞都為標題大小寫且至少包含一個字元,則 Python 字串istitle() 方法返回 True,否則返回 False。

示例

以下是 Python 字串istitle() 方法的示例。在此程式中,建立了一個字串 "Tutorialspoint!",並呼叫istitle() 函式來檢查輸入字串是否為標題大小寫。

str = "Tutorialspoint!"
result=str.istitle()
print("Is the input string titlecased?", result)

執行上述程式後,將生成以下輸出:

Is the input string titlecased? True

示例

標題大小寫表示小寫字母始終後跟大小寫字元,大寫字母始終後跟非大小寫字元。

str = "TutorialsPoint!"
result=str.istitle()
print("Is the input string titlecased?", result)

執行上述程式後獲得以下輸出:

Is the input string titlecased? False

示例

為了使istitle() 方法返回 True,輸入字串的每個單詞都必須是標題大小寫。

str = "Hello world"
result=str.istitle()
print("Is the input string titlecased?", result)

執行上述程式後獲得以下輸出:

Is the input string titlecased? False

示例

即使建立的字串中只有一個單詞不是標題大小寫,Python 字串istitle() 方法也會返回 False。

str = "Hello!Welcome To TUTORIALSPOINT."
result=str.istitle()
print("Is the input string titlecased?", result)

執行上述程式後,將顯示以下輸出:

Is the input string titlecased? False

示例

建立的字串也可以包含數字。但是,為了使其成為標題大小寫,數字之後只能是大寫字元。

str = "123Hello World."
result=str.istitle()
print("Is the input string titlecased?", result)

上述程式的輸出如下所示:

Is the input string titlecased? True
python_strings.htm
廣告