3 ms·
[::-1] is an idiomatic way to reverse a string in Python that is the obvious way for a habitual user of the language to do it e.g.: def palindrome(s):
by d0mine 11y ago
[::-1] is an idiomatic way to reverse a string in Python that is the obvious way for a habitual user of the language to do it e.g.:
def palindrome(s):
return s == s[::-1]
It could be discussed whether ''.join(reversed(s)) is more readable for a novice programmer learning Python. In general, Python prefers words over punctuation.
Also, there are objects that can be reversed() that are not sequences.
http://stackoverflow.com/questions/931092/reverse-a-string-in-python http://stackoverflow.com/questions/931092/reverse-a-string-i...