How is membership testing done in Python for strings?

In Python, membership testing for strings can be done using the in keyword. This allows you to check if a substring exists within a string. Here’s an example:

# Example string
text = "Hello, welcome to Python programming!"

# Membership testing
substring = "Python"
if substring in text:
    print(f"'{substring}' is found in the text.")
else:
    print(f"'{substring}' is not found in the text.")

In this example, the code checks if the substring "Python" is present in the text string and prints the appropriate message based on the result.

0 Comments

no data
Be the first to share your comment!