python字符串反转方法_用Python反转字符串的5种方法

python字符串反转方法_用Python反转字符串的5种方法_第1张图片

python字符串反转方法

Hello everyone, in this tutorial we’ll see different ways to reverse string in Python.

大家好,在本教程中,我们将看到在Python中反转字符串的不同方法。

As we know, we can reverse a list using reverse() method but Python doesn’t have the reverse() method for string.

众所周知,我们可以使用reverse()方法来反转 列表,但是Python没有用于字符串reverse()方法

Here are some alternate and easy ways to reverse a string.

这是一些可逆的简单方法。

在Python中反转字符串的方法 (Ways to Reverse String in Python)

1.使用循环 (1. Using Loop)

string1 =  "the crazy programmer"
string2 = ""
 
i = len(string1)-1
 
while(i>=0):
  string2 = string2 + string1[i]
  i = i-1
 
print "original = " + string1
print "reverse  = " + string2

Output:

输出:

original = the crazy programmer reverse = remmargorp yzarc eht

原始=疯狂的程序员 反向= remmargorp yzarc等

In above program, we’ve started a loop from the last index (length-1) to first index (0) of string1. In each step of loop, it will pick the character from right-side in string1 and concatenate with string2.

在上面的程序中,我们开始了从string1的最后一个索引(length-1)到第一个索引(0)的循环。 在循环的每个步骤中,它将从string1的右侧选择字符并与string2连接。

2.使用递归 (2. Using Recursion)

def reverse_it(string):
  if len(string)==0:
    return string
  else:
    return reverse_it(string[1:]) + string[0]
    print "added " + string[0]
 
string1 = "the crazy programmer"
string2 = reverse_it(string1)
 
print "original = " + string1
print "reversed = " + string2

In above program, there is a reverse_it() method which accepts a string and then it will check if the string is empty or not, if empty then it will return the string otherwise it will call itself by passing string from its second character to last character.

在上面的程序中,有一个reverse_it()方法接受一个字符串,然后它将检查该字符串是否为空,如果为空,则将返回该字符串,否则它将通过将字符串从第二个字符传递到最后一个字符来进行调用字符。

String = “hello”

字符串=“你好”

Print string[1:]

打印字符串[1:]

Output:  ‘ello’

输出:“ ello”

After calling reverse_it() method again and again there will be a point when string will be empty then the condition

一次又一次地调用reverse_it()方法之后,将有一个字符串为空的条件,

if len(string) == 0:

如果len(string)== 0:

will be true , it will return the string.  return statement will throw the execution, where it came from.

将为true,它将返回字符串。 return语句将抛出执行源。

So in

所以在

Return reverse_it(string[1:])  +  string[0]

返回reverse_it(string [1:])+ string [0]

the “+ string[0] “ will be executed next, which will add the first letter at last.

接下来将执行“ + string [0]” ,最后将添加第一个字母。

3.使用堆栈 (3. Using Stack)

def create_stack():
  #it will  create a List named as stack and return it
  stack = []
  return stack
 
def push(stack,element):
  #it will add a new element to List
  stack.append(element)
 
def pop(stack):
  #it will delete the last element from  List
  if len(stack) == 0:
    return
  return stack.pop()
 
def reverse(string):
 
  #method to reverse the string using stack's functions
  n = len(string)
  
  #to create a empty list (stack)
  stack = create_stack()
 
  #inserting character of string into List
  for i in range(0,n):
    push(stack,string[i])
 
  #making string empty
  string = ""
 
  #getting last element of the List (stack) and storing it into string
  for i in range(0,n):
    string = string + pop(stack)
  return string
 
string1 = "the crazy programer"
string2 = reverse(string1)
 
print "original = " + string1
print "reversed = " + string2

In above program, we’re using concept of stack having push and pop functions.

在上面的程序中,我们使用具有推入和弹出功能的堆栈概念。

To implement stack concept we’re using list.

为了实现堆栈概念,我们使用列表。

When we call reverse() method, it will create a list named as ‘stack’ and insert all the characters of string into list using push() method. At last it will fetch all the elements in the list from last to first one by one and store them into the string.

当我们调用reverse()方法,它会创建一个使用push()方法命名为“叠加”和插入的所有字符串的字符到列表清单 。 最后,它将从上到下依次提取列表中的所有元素,并将它们存储在字符串中。

4.使用扩展切片 (4. Using Extended Slice)

string = "the crazy programmer"
print "original = " + string
 
string = string[::-1]
print "reversed = " + string

Mostly extended slice is used for skipping the steps but if we put -1 in third ‘step’ or ‘stride’ argument then we can get the reverse of a string, list and tupple.

大多数情况下,扩展切片用于跳过步骤,但是如果我们在第三个“ step”“ stride”参数中输入-1,则可以得到字符串,列表和tupple的倒序。

5.使用清单 (5. Using List)

string = "the crazy programmer"
print "original = " + string
 
#convrting string into list
list1 = list(string)
 
#applying reverse method of list
list1.reverse()
 
#converting list into string
string = ''.join(list1)
print "reversed = " + string

String doesn’t have reverse() method but lists have. So we are converting string into list, performing reverse() operation and again converting it back into string using ‘ ’.join() method.

字符串没有reverse()方法,但列表具有。 因此,我们将字符串转换为列表,执行reverse()操作,然后再次使用''.join()方法将其转换回字符串。

Comment below if you have queries or know any other way to reverse a string in python.

如果您有疑问或知道以其他方式在python中反转字符串,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/12/reverse-string-python.html

python字符串反转方法

你可能感兴趣的:(字符串,列表,堆栈,python,java)