# File: strmisc.py # Do some miscellaneous things with strings # # Matt Bishop, ECS 10, Spring 2014 # # First, count substrings instr = input("Enter a string: ") # put quotes around the string to show its boundaries when it is printed prstr = '"' + instr + '"' # get the substring to find ctstr = input("What substring should I count? ") # put quotes around the string to show its boundaries when it is printed prctstr = '"' + ctstr + '"' # now print the count count = instr.count(ctstr) print(prstr, "contains the string", prctstr, count, "times") # # does it start and/or end with the string? # if instr.startswith(ctstr): print(prstr, "begins with", prctstr) if instr.endswith(ctstr): print(prstr, "ends with", prctstr) # # now do a replacement # repstr = instr.replace(ctstr, "XXX") prrepstr = '"' + repstr + '"' print("Replacing", ctstr, "with XXX in", prstr, "gives", prrepstr)