PCAP File I/O and String Operations 2 — Questions and Answers
Question 1: Which string method converts all characters to uppercase?
- capitalize()
- upper() (Correct answer)
- title()
- swapcase()
Correct answer: upper()
`str.upper()` returns a new string with all characters converted to uppercase.
Question 2: What does `str.strip()` do?
- Splits the string
- Removes leading and trailing whitespace (Correct answer)
- Reverses the string
- Converts to lowercase
Correct answer: Removes leading and trailing whitespace
`strip()` removes whitespace (or specified characters) from both ends of the string.
Question 3: How do you split a string `s` by commas?
- s.split(',') (Correct answer)
- s.divide(',')
- split(s, ',')
- s.cut(',')
Correct answer: s.split(',')
`str.split(delimiter)` splits the string at each occurrence of the delimiter and returns a list.
Question 4: Which method joins a list of strings with a separator?
- list.join(sep)
- sep.join(list) (Correct answer)
- str.concat(list, sep)
- merge(list, sep)
Correct answer: sep.join(list)
`separator.join(iterable)` concatenates iterable elements using the separator string.
Question 5: What does the f-string `f'Hello {name}'` do?
- Creates a raw string
- Inserts the value of `name` into the string (Correct answer)
- Formats a float
- Encodes the string to bytes
Correct answer: Inserts the value of `name` into the string
f-strings (formatted string literals) evaluate expressions in `{}` and embed them in the string.
Question 6: Which method checks if a string starts with a given prefix?
- startswith() (Correct answer)
- beginswith()
- prefix()
- starts()
Correct answer: startswith()
`str.startswith(prefix)` returns True if the string starts with the specified prefix.
Which string method converts all characters to uppercase?