Correct option is D
In Python, there are multiple ways to reverse a string:
1. word[::-1]:
· This uses slicing with a step of -1 to reverse the string.
2. reversed(word):
· This returns a reversed iterator, which can be converted to a string using "".join().
3. "".join(reversed(word)):
· This is another valid method that combines the reversed iterator into a single string.
Important Key Points:
1. Strings in Python are immutable, so reversing them does not modify the original string.
2. The slicing method ([::-1]) is the most concise and widely used for reversing strings.
3. Using reversed() with "".join() is more explicit but slightly longer.
Knowledge Booster:
· word.reverse(): This method is not valid for strings as it works only for mutable sequences like lists.
· Strings can also be reversed using loops, but Python's built-in methods are preferred for simplicity and readability.