Common String Tasks

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.

  • MOST common task is probably formatting strings from non-string data.
  • Splitting and processing the resulting "tokens" somehow (e.g., manually reading CSV files, finding words in a larger body of text, etc). Or combining strings. We cover both of these in Joining and Splitting Strings.
  • Simple fix-up user input (trim, toupper(), etc.) See below.
  • Search and replace (with and without regular expressions).
  • Validation against regular expressions.
  • Does a string contain another string. (See below).

Tip: Quick checks for strings from untrusted sources

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.

In [1]:
# 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 
if(user_entered_state and user_entered_state.strip().casefold() == 'CA'.casefold()):
    print("You're in California!")
You're in California!

Checking whether / where strings are in other strings

In [2]:
# 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"))