2) c. Strings

String variables hold collections of characters. In other words they hold text. Unlike some other programming languages, you can use either single or double quotes to create string literals. However, it is important for code readability to choose one style and stick with it.



String Manipulation:

Suppose you have two strings. How do you find out whether both of them are the same? You can do this by directly comparing them like how you would do for numbers. Use use the double equals ‘==’ operator.

1
2
Output:
True

But what if it you wanted to do a case insensitive comparison? So that Aladdin and aladdin would both be the same. You can do this by converting the strings to lower case before comparing them using the lower() function.

1
2
3
4
5
Output:
Checking if the visitor is a thief
False
Checking with case insensitive comparison
True



You can replace a part of a string with another one using the convenient replace() method. The way to use this is as follows:

replace(“existing text”, “new text”)

Here’s an example to illustrate this:

1
2
3
Output:
New paragraph:
Towards you I roll, you all-destroying but unconquering whale; to the last I grapple with you; from hell's heart I stab at you; for hate's sake I spit my last breath at you. - Herman Melville, Moby Dick





Suppose you had a string that was a long paragraph. And you wanted to divide it into smaller pieces, or cut them into sentences and pick up the first sentence. How would you do that?

Then you could “split” the whole paragraph based on the period character, (you could use anything, not just a period character). Imagine splitting a baguette into multiple pieces. Then, you could choose the first piece by specifying the index.

Check out the following program:

1
2
Output:
I wanted you to see what real courage is, instead of getting the idea that courage is a man with a gun in his hand



Let’s say that you have a string that contains a file name. You want to check whether this file name contains the word “1080p” in it. How can you check whether a string exists in another?

Use the in built-in to check. You get back a True if the string is found or a False if it’s not found.

Alternatively, you can use the find() method for the exact index where the match is found. If no match is found then it gives back a -1 value.

1
2
3
4
Does this filename contain '1080p'? =
True
The index of 1080p =
27



Slicing Strings:

You should know how strings are stored in the memory. Knowing this will allow you to manipulate them more easily. Take a look at the diagram below:




Strings are simply a sequence of characters that are stored sequentially.

The “index” shown in the diagram is only to illustrate the serial numbers of each character block. You can essentially refer to the individual blocks by their index numbers.

For example str1[0] would yield the character ‘H’. Here is an example that illustrates this:

1
2
3
Output:
A
e




The benefit of being able to access the individual characters using their indexes is that you can “slice” the strings in Python and extract the substring that you need. Here are a few ways of slicing in Python:

1
2
3
Output:
Pieces of eight
i




Notice that if you use sentence[2:4] then the extracted word starts from (and including) index# 2 and stops at (but excluding) index#4.

This concept can be very confusing for beginners.

However, an easy trick to not get confused is to remember that slicing always occurs before the indexes. Here is a diagram that illustrates this:



Summary of methods used:

  • upper()
  • lower()
  • replace(“existing text”, “new text”)
  • str1 == str2
  • str1 in str2
  • find()
  • split()
  • str[m:n] (slicing)