Home  >  Article  >  Backend Development  >  Reverse output of a string using Python

Reverse output of a string using Python

anonymity
anonymityOriginal
2019-05-25 13:31:095751browse

Python method to reverse output string

Reverse output of a string using Python

Method 1: Use list reversed function

class Solution(object):
 def reverse_string(self, s):
  if len(s) > 1:
   reversed_s = ''.join(reversed(s))
   return reversed_s 
  return s

Method 2: Use sharding (commonly used)

def string_reverse(str): 
    return str[::-1]

The above is the detailed content of Reverse output of a string using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn