PCEP PCEP String Operations and Methods 1 — Questions and Answers
Question 1: What is the output of `'Python'[1:4]`?
- yth (Correct answer)
- Pyt
- ytho
- ython
Correct answer: yth
Slicing `[1:4]` extracts characters at indices 1, 2, and 3, giving 'yth'.
Question 2: Which string method returns `True` if all characters in the string are alphabetic?
- isalpha() (Correct answer)
- isdigit()
- isalnum()
- isspace()
Correct answer: isalpha()
`isalpha()` returns `True` only when every character in the string is a letter.
Question 3: What does `'hello'.upper()` return?
- HELLO (Correct answer)
- Hello
- hello
- hELLO
Correct answer: HELLO
The `upper()` method converts all characters in the string to uppercase.
Question 4: What is the result of `' hello '.strip()`?
- hello (Correct answer)
- ' hello '
- hello
- hello
Correct answer: hello
`strip()` removes leading and trailing whitespace from a string.
Question 5: Which operator is used to repeat a string in Python?
- * (Correct answer)
- +
- **
- %
Correct answer: *
The `*` operator repeats a string a given number of times, e.g., `'ab' * 3` gives `'ababab'`.
Question 6: What does `'apple,banana,cherry'.split(',')` return?
- ['apple', 'banana', 'cherry'] (Correct answer)
- ('apple', 'banana', 'cherry')
- apple banana cherry
- {'apple', 'banana', 'cherry'}
Correct answer: ['apple', 'banana', 'cherry']
`split(',')` breaks the string at each comma and returns a list of the resulting substrings.
What is the output of `'Python'[1:4]`?