2016 September 14,

CS 111: Strings

In addition to integers, floating-point numbers, images, and videos, computers also commonly process text. Here are some important terms. A character is a letter, digit, punctuation mark, or any other individual symbol that is used in text. Spaces, tabs, carriage returns, and other forms of "empty space" in text also count as characters; they are called white space characters. A string is a sequence of zero or more characters. You usually enter a string in Python by enclosing it in single quotation marks or double quotation marks:

myName = 'Lucy'

myName

yourName = "Leroy Godzilla, Esq."

yourName

You can ask a string for its length like this:

len("Carleton")

len(myName)

The [] operator is used to extract particular characters from strings, as in the following example.

name = "Captain Haddock"

name[1]

name[4]

name[3]

Question 05A: How do you extract the first character from a string? What happens if you ask for a character after the last character? Suppose you have a string stored in a variable hisName. How can you use len() and [] together to extract the last character from hisName?

Question 05B: What happens if you ask for the character at index -1 from hisName? What about the character at index -2?

The [] operator can be used to extract not just single characters but also entire substrings, as follows.

name = "Shalikashvili"

name[3:6]

name[0:4]

Question 05C: When you ask for name[3:6], exactly which characters are you getting out of name? What about when you ask for name[0:4]? Be careful.

Question 05D: What happens if you "add" two strings together, using the + operator? What happens if you try to subtract two strings? Why?

I promised you in an earlier tutorial that all data inside a computer are numeric. Is that true of strings too? Yes. The characters are paired with integers in a particular order, according to an encoding scheme such as ASCII or Unicode. You can discover how to translate between characters and integers using chr and ord, like this:

ord("A")

chr(97)

Question 05E: To which integer does the character 'T' correspond? To which character does the integer 37 correspond?

Question 05F: Execute the following lines of code and explain why Python responds as it does.

'f' > 'a'

'f' > 'A'

'F' > 'a'

'F' > 'A'

Question 05G: Suppose that x is a string variable storing a single lower-case character. Give me code that produces the upper-case version of x. (This is a multi-step problem, requiring some investigation on your part.)