As a programmer, the most common things I've done with strings over the years have nothing to do with the sort of problems you'll encounter in programming interviews.
Two things I almost always do before comparing strings is to fix up the source string by removing unecessary whitespace and making the string of a known case. I may also include a check for None first.
# For example assume you have this but it's coming from somewhere else in your system so it's not # that obvious what's really in it. user_entered_state = 'ca ' # Check that it's not None, there's no whitespace, and case is consistent: # For why casefold and not just ".upper()" see # https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison if(user_entered_state and user_entered_state.strip().casefold() == 'CA'.casefold()): print("You're in California!")
You're in California!
# If you don't care where: print("needle" in "is there a needle here?") print("needle" in "a haystack full of hay") # If you need to know where: print ("is there a needle here?".find("needle")) print ("a haystack full of hay".find("needle"))
True False 11 -1