Python中的反转字符串(5种不同的方式)
Python字符串库不支持其他Python容器(如 list)内置的“ reverse() ”,因此了解其他反转字符串的方法可能是有用的。本文讨论了实现它的几种方法。
使用循环
# 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
解释:扩展切片提供了一个“step”字段作为[start,stop,step] ,并且没有给出任何字段作为 start 和 stop 分别表示默认为 0 和字符串长度,“ -1 ”表示从 end 开始并在开始,因此反转字符串。
使用反向
# 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() 分隔的空字符串。并形成逆序字符串。