String Manipulation and Text Processing Quiz Quiz

  1. Finding Substrings

    In Python, what is the best method to check if the substring 'cat' exists in the string variable text?

    1. if 'cat' in text:
    2. text.contains('cat')
    3. if text.has('cat'):
    4. if text == 'cat':
    5. text.exist('cat')
  2. Replacing Text

    Given s = 'Hello World', which method replaces 'World' with 'Universe'?

    1. s.replace('World', 'Universe')
    2. s.swap('World', 'Universe')
    3. s.replaceAll('World', 'Universe')
    4. s.edit('World', 'Universe')
    5. replace(s, 'World', 'Universe')
  3. Splitting Strings

    How do you split a string by commas in Python and store the result in a list?

    1. sentence.split(',')
    2. sentence.split('.')
    3. sentence.divide(',')
    4. split(sentence, ',')
    5. sentence.splitting(',')
  4. Joining Strings

    Given a list words = ['a', 'b', 'c'], which code joins them into the string 'a-b-c'?

    1. '-'.join(words)
    2. join(words, '-')
    3. words.join('-')
    4. '-'.add(words)
    5. implode('-', words)
  5. Counting Substrings

    Which method counts the number of times 'the' appears in a string s?

    1. s.count('the')
    2. s.find('the')
    3. s.search('the')
    4. s.len('the')
    5. s.counter('the')
  6. Changing Case

    How do you convert all characters in a string s to lowercase?

    1. s.lower()
    2. s.toLowercase()
    3. toLower(s)
    4. s.lowercase()
    5. s.makeLower()
  7. Stripping Whitespace

    Given str1 = ' hello ', which code removes spaces at both ends?

    1. str1.strip()
    2. str1.lstrip()
    3. str1.rstrip()
    4. trim(str1)
    5. str1.strip(' ')
  8. Finding Index of Character

    If s = 'banana', what code returns the first index of 'a'?

    1. s.index('a')
    2. s.findFirst('a')
    3. find(s, 'a')
    4. s.locate('a')
    5. s.pos('a')
  9. Removing Non-Alpha Characters

    What is the Python code to remove all non-letter characters from s = 'abc123!' using regular expressions?

    1. re.sub(r'[^a-zA-Z]', '', s)
    2. re.replace(r'a-zA-Z', '', s)
    3. re.remove('[^a-zA-Z]', s)
    4. re.sub('[a-zA-Z]', '', s)
    5. replace(s, r'[^a-zA-Z]', '')
  10. String Formatting

    Which code correctly formats name and age into the string: 'Alice is 30 years old'?

    1. f'{name} is {age} years old'
    2. '{} is {} years old'.format(name age)
    3. '%s is %d years old' % (name, age)
    4. f'{name} are {age} years old'
    5. name + ' is ' + str(age) ' years old'