Python字符串库不支持内置的“ 反向 “正如列表等其他python容器所做的那样,因此了解其他反转字符串的方法可能会很有用。本文讨论了几种实现方法。
# Python code to reverse a string # using loop 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)) |
输出:
The original string is : Geeksforgeeks The reversed string(using loops) is : skeegrofskeeG
说明: 在上面的代码中,我们调用一个函数来反转字符串,该字符串将智能地迭代到每个元素 在开头加入每个角色 以获得反向字符串。
# Python code to reverse a string # using recursion 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)) |
输出:
The original string is : Geeksforgeeks The reversed string(using recursion) is : skeegrofskeeG
说明: 在上面的代码中,字符串作为参数传递给递归函数,以反转字符串。在函数中,基本条件是,如果字符串的长度等于0,则返回字符串。如果不等于0,则递归调用reverse函数来分割字符串中除第一个字符以外的部分,并将第一个字符连接到被分割字符串的末尾。
# Python code to reverse a string # using stack # 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)) |
输出:
The original string is : Geeksforgeeks The reversed string(using stack) is : skeegrofskeeG
说明: 将创建一个空堆栈。一个接一个的字符串被推送到堆栈中。 一个接一个地弹出堆栈中的所有字符,并将它们放回字符串。
# Python code to reverse a string # using extended slice syntax # 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)) |
输出:
The original string is : Geeksforgeeks The reversed string(using extended slice syntax) is : skeegrofskeeG
说明: Extended slice提供了一个“step”字段作为 [开始,停止,迈步] ,并且不提供任何字段作为开始和停止,分别表示默认值为0和字符串长度,以及“ -1 “表示从末端开始并在开始处停止,因此反转字符串。
# 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)) |
输出:
The original string is : Geeksforgeeks The reversed string(using reversed) is : skeegrofskeeG
说明: reversed()返回给定字符串的反向迭代器,然后使用join()将其元素连接到空字符串中。并形成逆序字符串。
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。