La biblioteca de strings de Python no es compatible con » reverse() » incorporado como lo hacen otros contenedores de Python como list, por lo tanto, conocer otros métodos para invertir strings puede resultar útil. Este artículo analiza varias formas de lograrlo en Python .
Ejemplo:
Python3
def reverse(s): str = "" for i in s: str = i + str return str s = "Geeksforgeeks" print("The original string is : ", end="") print(s) print("The reversed string(using loops) is : ", end="") print(reverse(s))
Python3
def reverse(s): if len(s) == 0: return s else: return reverse(s[1:]) + s[0] s = "Geeksforgeeks" print("The original string is : ", end="") print(s) print("The reversed string(using recursion) is : ", end="") print(reverse(s))
Python3
# Function to create an empty stack. It # initializes size of stack as 0 def createStack(): stack = [] return stack # Function to determine the size of the stack def size(stack): return len(stack) # Stack is empty if the size is 0 def isEmpty(stack): if size(stack) == 0: return true # Function to add an item to stack . It # increases size by 1 def push(stack, item): stack.append(item) # Function to remove an item from stack. # It decreases size by 1 def pop(stack): if isEmpty(stack): return return stack.pop() # A stack based function to reverse a string def reverse(string): n = len(string) # Create a empty stack stack = createStack() # Push all characters of string to stack for i in range(0, n, 1): push(stack, string[i]) # Making the string empty since all # characters are saved in stack string = "" # Pop all characters of string and put # them back to string for i in range(0, n, 1): string += pop(stack) return string # Driver code s = "Geeksforgeeks" print("The original string is : ", end="") print(s) print("The reversed string(using stack) is : ", end="") print(reverse(s))
Python3
# Function to reverse a string def reverse(string): string = string[::-1] return string s = "Geeksforgeeks" print("The original string is : ", end="") print(s) print("The reversed string(using extended slice syntax) is : ", end="") print(reverse(s))
Python3
# Python code to reverse a string # using reversed() # Function to reverse a string def reverse(string): string = "".join(reversed(string)) return string s = "Geeksforgeeks" print("The original string is : ", end="") print(s) print("The reversed string(using reversed) is : ", end="") print(reverse(s))
Python3
# Function to reverse a string def reverse(string): string = [string[i] for i in range(len(string)-1, -1, -1)] return "".join(string) s = "Geeksforgeeks" print("The original string is : ", s) print("The reversed string(using reversed) is : ", reverse(s))
Python3
# Function to reverse a string # by converting string to list # then reversed it and again convert it to string def reverse(string): string = list(string) string.reverse() return "".join(string) s = "Geeksforgeeks" print("The original string is : ", s) print("The reversed string(using reversed) is : ", reverse(s)) # This code is contributed by Susobhan AKhuli
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA