了解如何使用Python字符串的”字符串判斷”功能

使用 Python 判斷字符串是否不僅包含特定子串

Python 是一種流行的編程語言,它可以用來做很多事情,其中一個是判斷字符串是否不僅包含特定子串。在 Python 中,有一個叫做 in 的操作符,可以用來檢查一個字符串是否包含另一個字符串。

例如,如果你想檢查一個字符串是否包含另一個字符串,你可以使用下面的代碼:

if "string" in "This is a string":
    print("The string contains the substring!")

上面的代碼會檢查字符串 “This is a string” 是否包含子串 “string”,如果包含,則會顯示 “The string contains the substring!”。

但是,如果你想檢查一個字符串是否不僅包含特定子串,你可以使用下面的代碼:

if "string" not in "This is a string":
    print("The string does not contain the substring!")

上面的代碼會檢查字符串 “This is a string” 是否不包含子串 “string”,如果不包含,則會顯示 “The string does not contain the substring!”。

總之,使用 Python 判斷字符串是否不僅包含特定子串非常簡單,只需要使用 in 操作符和 not in 操作符就可以了。

發佈留言