2  Fundamentals of Text Manipulation

Basic demonstrations of array indexing, string concatenation, splitting, and identifying capitalized words, in both Python and R.

3 Fundamentals

This section provides basic programming concepts that we’ll use when analyzing Romeo and Juliet and Metamorphosis. We will illustrate both Python and R.

3.1 1. Array (List) Indexing

3.1.1 Explanation

3.1.1.1 Python Example

# In Python, lists store sequences of items.
# Let's create a small list of words:
words = ["Hello", "world", "this", "is", "Python"]

# Accessing the first item:
print(words[0])  # "Hello"

# Accessing the last item:
print(words[-1]) # "Python"

# Slicing from index 1 to 3 (exclusive end):
print(words[1:4]) # ["world", "this", "is"]

3.1.1.2 R Example

# In R, vectors store sequences of items.
# Let's create a small character vector:
words <- c("Hello", "world", "this", "is", "R")

# Accessing the first item (indexing starts at 1 in R):
words[1]    # "Hello"

# Accessing the last item:
words[length(words)]  # "R"

# Slicing from index 2 to 4:
words[2:4]  # "world" "this" "is"

3.1.2 Interactive Exercise

Calculate the average of all of the integers from 1 to 10. Replace the ______ with the correct code.

3.1.2.1 Output


3.2 2. String Concatenation

3.2.1 Explanation

3.2.1.1 Python Example

greeting = "Hello"
target = "World"
combined = greeting + " " + target
print(combined)  # "Hello World"

3.2.1.2 R Example

greeting <- "Hello"
target <- "World"
combined <- paste(greeting, target)
combined  # "Hello World"

3.2.2 Interactive Exercise

Concatenate the strings "Hello" and "World" using the appropriate function or operator. Replace the ______ with the correct code.

3.2.2.1 Output


3.3 3. String Splitting

3.3.1 Explanation

3.3.1.1 Python Example

sentence = "Romeo, Romeo! wherefore art thou Romeo?"
split_words = sentence.split()
print(split_words)
# Expected: ['Romeo,', 'Romeo!', 'wherefore', 'art', 'thou', 'Romeo?']

3.3.1.2 R Example

sentence <- "Romeo, Romeo! wherefore art thou Romeo?"
split_words <- strsplit(sentence, split=" ")
split_words
# Expected: list(c("Romeo,", "Romeo!", "wherefore", "art", "thou", "Romeo?"))

3.3.2 Interactive Exercise

Split the sentence "Romeo, Romeo! wherefore art thou Romeo?" into words. Replace the ______ with the correct code.

3.3.2.1 Output


3.4 4. Checking If a String Is a Proper Noun

3.4.1 Explanation

A proper noun here is defined as a word that starts with a capital letter (A–Z) followed by lowercase letters (a–z).

3.4.1.1 Python Example

def is_proper_noun(word):
    if len(word) == 0:
        return False
    return word[0].isupper() and word[1:].islower()

# Tests:
print(is_proper_noun("Romeo"))   # True
print(is_proper_noun("ROMEO"))   # False
print(is_proper_noun("romance")) # False

3.4.1.2 R Example

is_proper_noun <- function(word) {
  if (nchar(word) == 0) {
    return(FALSE)
  }
  first_char <- substr(word, 1, 1)
  rest_chars <- substr(word, 2, nchar(word))
  return(grepl("^[A-Z]$", first_char) && grepl("^[a-z]*$", rest_chars))
}

# Tests:
is_proper_noun("Romeo")   # TRUE
is_proper_noun("ROMEO")   # FALSE
is_proper_noun("romance") # FALSE

3.4.2 Interactive Exercise

Implement the function is_proper_noun to determine if a given word is a proper noun. Replace the ______ with your implementation.

3.4.2.1 Output