Python | Check if substring present in string

Python | Check if substring present in string

To check if a substring is present in a string in Python, you can use the in keyword. Here's how you can do it:

def substring_present(substring, main_string): return substring in main_string # Example usage main_string = "Hello, world!" substring = "world" if substring_present(substring, main_string): print(f"'{substring}' is present in '{main_string}'") else: print(f"'{substring}' is not present in '{main_string}'") 

In the code above, the substring_present function checks if the substring is present in the main_string and returns True if it is, or False otherwise.

The in keyword checks for the presence of the substring and is a simple and efficient way to determine if one string contains another.


More Tags

components command-line-interface 32-bit google-maps-sdk-ios formatted-input greatest-common-divisor networkimageview libusb-1.0 bind centos

More Programming Guides

Other Guides

More Programming Examples