Python strings come with many built-in methods - functions you can call directly on any string. These methods let you transform text, search for patterns, and clean up data. The best part? You can often guess what they do from their names - upper() makes text uppercase, replace() replaces text, and so on.
message = "I love Python programming with Python"# Check if something existsprint("Python" in message) # Trueprint(message.startswith("I")) # Trueprint(message.endswith("Python")) # True# Find positionprint(message.find("Python")) # 7 (first occurrence)print(message.count("Python")) # 2 (number of times)# Replacenew_message = message.replace("Python", "JavaScript")print(new_message) # "I love JavaScript programming with JavaScript"
Python has over 40 string methods! We’ve covered the most common ones here. For a complete list, check the official Python documentation.