-
Python:
pants = "I am wearing pants." # Method 1: Check if the string contains the word "pants" if "pants" in pants: print("I am wearing pants.") else: print("I am not wearing pants.") # Method 2: Replace the word "pants" with "trousers" pants = pants.replace("pants", "trousers") print(pants) # Method 3: Split the string into a list of words and check if "pants" is present word_list = pants.split() if "pants" in word_list: print("Pants found in the string.") -
JavaScript:
var pants = "I am wearing pants."; // Method 1: Check if the string contains the word "pants" if (pants.includes("pants")) { console.log("I am wearing pants."); } else { console.log("I am not wearing pants."); } // Method 2: Replace the word "pants" with "trousers" pants = pants.replace("pants", "trousers"); console.log(pants); // Method 3: Split the string into an array of words and check if "pants" is present var wordArray = pants.split(" "); if (wordArray.includes("pants")) { console.log("Pants found in the string."); }