""" Purpose: consider string replace() functions """ # Get text, search string, and replacement string text = input( "Enter text: " ) s = input( "Enter substring (s): " ) # this search is case sensitive... A =/= a r = input( "Enter substring (r): " ) # if you just hit enter, the empty string "" will be stored in r # replace() will only modify whitespace if you ask it to # if there are no instances of s in text, nothing will chance print() # "" =/= " " the empty string is not the same as a space - there are empty strings between each character # Get version of text with occurrences of s replaced with r modified_text = text.replace( s, r ) # text is still the same, we have to capture that new string in # modified_text in an assignment statement because strings are immutable # Print substitution print( "text.replace( s, r ):", modified_text, " # text's s's replaced with r's" ) # how are we going to use the replace() function? # parsing phone numbers, everyone stores phone numbers different ways with dashes,spaces,parentheses # replace() can be really handy in cleaning up input # to only replace part of a string, we can slice it and only operate on a substring # if you want to replace multiple things, you need to use separate replace() statements... or look at regex # regular expressions (regex) looks for patterns, so you can look for only numbers or something # CS 3102 works with regex