What keyword is used to check if a string contains a substring in Python?

Master Python with the PCAP Certification! Explore interactive quizzes and detailed explanations to ensure your exam success. Gain confidence and get certified!

In Python, the keyword used to check if a string contains a substring is "in." This operator allows you to test for membership within strings, collections, and other iterable objects. When you use it, you can write expressions like "substring" in "main string", which will return True if the substring exists within the main string, and False otherwise.

For example, if you have the following code:


text = "Hello, world!"

result = "world" in text

print(result)  # This will print: True

Here, the expression evaluates to True because "world" is indeed a substring of "Hello, world!".

The other choices, while they may evoke notions of searching or membership, do not serve as valid keywords or operators in Python for this specific purpose. Using them would result in errors if implemented within a Python program, as they are not recognized by the language's syntax or semantics.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy