What's the most efficient way to reverse the string and swap the case? For example, if my input string is:
input_string = "Hello Python World"
I want the content of the string to be:
output_string = "DLROw NOHTYp OLLEh"
Join Stack Overflow to learn, share knowledge, and build your career.
What's the most efficient way to reverse the string and swap the case? For example, if my input string is:
input_string = "Hello Python World"
I want the content of the string to be:
output_string = "DLROw NOHTYp OLLEh"
You may swap the case using str.swapcase()
on the reversed string (or vice-versa) as:
input_string = "Hello Python World"
output_string = input_string[::-1].swapcase()
# to reverse the string ^
# OR, input_string.swapcase()[::-1]
where output_string
will hold:
>>> output_string
'DLROw NOHTYp OLLEh'
Something like
''.join([c.lower() if c.isupper() else c.upper() for c in my_string][::-1])