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:
= ["Hello", "world", "this", "is", "Python"]
words
# 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:
<- c("Hello", "world", "this", "is", "R")
words
# Accessing the first item (indexing starts at 1 in R):
1] # "Hello"
words[
# Accessing the last item:
length(words)] # "R"
words[
# Slicing from index 2 to 4:
2:4] # "world" "this" "is" words[
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
= "Hello"
greeting = "World"
target = greeting + " " + target
combined print(combined) # "Hello World"
3.2.1.2 R Example
<- "Hello"
greeting <- "World"
target <- paste(greeting, target)
combined # "Hello World" combined
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
= "Romeo, Romeo! wherefore art thou Romeo?"
sentence = sentence.split()
split_words print(split_words)
# Expected: ['Romeo,', 'Romeo!', 'wherefore', 'art', 'thou', 'Romeo?']
3.3.1.2 R Example
<- "Romeo, Romeo! wherefore art thou Romeo?"
sentence <- strsplit(sentence, split=" ")
split_words
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
<- function(word) {
is_proper_noun if (nchar(word) == 0) {
return(FALSE)
}<- substr(word, 1, 1)
first_char <- substr(word, 2, nchar(word))
rest_chars 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.