Finding Substrings
In Python, what is the best method to check if the substring 'cat' exists in the string variable text?
- if 'cat' in text:
- text.contains('cat')
- if text.has('cat'):
- if text == 'cat':
- text.exist('cat')
Replacing Text
Given s = 'Hello World', which method replaces 'World' with 'Universe'?
- s.replace('World', 'Universe')
- s.swap('World', 'Universe')
- s.replaceAll('World', 'Universe')
- s.edit('World', 'Universe')
- replace(s, 'World', 'Universe')
Splitting Strings
How do you split a string by commas in Python and store the result in a list?
- sentence.split(',')
- sentence.split('.')
- sentence.divide(',')
- split(sentence, ',')
- sentence.splitting(',')
Joining Strings
Given a list words = ['a', 'b', 'c'], which code joins them into the string 'a-b-c'?
- '-'.join(words)
- join(words, '-')
- words.join('-')
- '-'.add(words)
- implode('-', words)
Counting Substrings
Which method counts the number of times 'the' appears in a string s?
- s.count('the')
- s.find('the')
- s.search('the')
- s.len('the')
- s.counter('the')
Changing Case
How do you convert all characters in a string s to lowercase?
- s.lower()
- s.toLowercase()
- toLower(s)
- s.lowercase()
- s.makeLower()
Stripping Whitespace
Given str1 = ' hello ', which code removes spaces at both ends?
- str1.strip()
- str1.lstrip()
- str1.rstrip()
- trim(str1)
- str1.strip(' ')
Finding Index of Character
If s = 'banana', what code returns the first index of 'a'?
- s.index('a')
- s.findFirst('a')
- find(s, 'a')
- s.locate('a')
- s.pos('a')
Removing Non-Alpha Characters
What is the Python code to remove all non-letter characters from s = 'abc123!' using regular expressions?
- re.sub(r'[^a-zA-Z]', '', s)
- re.replace(r'a-zA-Z', '', s)
- re.remove('[^a-zA-Z]', s)
- re.sub('[a-zA-Z]', '', s)
- replace(s, r'[^a-zA-Z]', '')
String Formatting
Which code correctly formats name and age into the string: 'Alice is 30 years old'?
- f'{name} is {age} years old'
- '{} is {} years old'.format(name age)
- '%s is %d years old' % (name, age)
- f'{name} are {age} years old'
- name + ' is ' + str(age) ' years old'