String Methods — Part 2
More powerful string methods for searching, replacing, and testing string content.
replace() & find()
s = "I love cats and cats love me"
print(s.replace("cats", "dogs"))
# "I love dogs and dogs love me"
print(s.replace("cats", "dogs", 1))
# "I love dogs and cats love me"
print(s.find("cats")) # 7
print(s.rfind("cats")) # 16
print(s.find("fish")) # -1 (not found)count()
print(s.count("cats")) # 2
print(s.count("love")) # 2startswith() & endswith()
url = "https://example.com"
print(url.startswith("https")) # True
print(url.endswith(".com")) # True
print(url.startswith(("http", "https"))) # TrueOther Useful Methods
print("abc123".isalnum()) # True
print(" ".isspace()) # True
print("HELLO".isupper()) # True
print("hello".isalpha()) # True