python字符串反向输出_Python反向字符串– 5种方法和最佳方法

python字符串反向输出

Python String doesn’t have a built-in reverse() function. However, there are various ways to reverse a string in Python.

Python String没有内置的reverse()函数。 但是,有多种方法可以在Python中反转字符串。

1.如何在Python中反转字符串? (1. How to Reverse a String in Python?)

Some of the common ways to reverse a string are:

反转字符串的一些常见方法是:

  • Using Slicing to create a reverse copy of the string.

    使用切片来创建字符串的反向副本。

  • Using for loop and appending characters in reverse order

    使用for循环并以相反顺序附加字符

  • Using while loop to iterate string characters in reverse order and append them

    使用while循环以相反的顺序迭代字符串字符并追加它们

  • Using string join() function with reversed() iterator

    反向()迭代器中使用字符串join()函数

  • Creating a list from the string and then calling its reverse() function

    从字符串创建列表 ,然后调用其reverse()函数

  • Using Recursion

    使用递归

1.1)使用切片的Python反向字符串 (1.1) Python Reverse String using Slicing)

def reverse_slicing(s):
    return s[::-1]

input_str = 'ABç∂EF'

if __name__ == "__main__":
    print('Reverse String using slicing =', reverse_slicing(input_str))

If you run above Python script, the output will be:

如果您在Python脚本上运行,则输出将是:

Reverse String using slicing = FE∂çBA

1.2)使用For循环反向字符串 (1.2) Reverse String using For Loop)

def reverse_for_loop(s):
    s1 = ''
    for c in s:
        s1 = c + s1  # appending chars in reverse order
    return s1

input_str = 'ABç∂EF'

if __name__ == "__main__":
    print('Reverse String using for loop =', reverse_for_loop(input_str))

Output: Reverse String using for loop = FE∂çBA

输出: Reverse String using for loop = FE∂çBA

1.3)使用While循环反转字符串 (1.3) Reverse a String using While Loop)

def reverse_while_loop(s):
    s1 = ''
    length = len(s) - 1
    while length >= 0:
        s1 = s1 + s[length]
        length = length - 1
    return s1

input_str = 'ABç∂EF'

if __name__ == "__main__":
    print('Reverse String using while loop =', reverse_while_loop(input_str))

1.4)使用join()和reversed()反转字符串 (1.4) Reverse a String using join() and reversed())

def reverse_join_reversed_iter(s):
    s1 = ''.join(reversed(s))
    return s1

1.5)使用列表reverse()的Python反向字符串 (1.5) Python Reverse String using List reverse())

def reverse_list(s):
    temp_list = list(s)
    temp_list.reverse()
    return ''.join(temp_list)

1.6)使用递归的Python反向字符串 (1.6) Python Reverse String using Recursion)

def reverse_recursion(s):
    if len(s) == 0:
        return s
    else:
        return reverse_recursion(s[1:]) + s[0]

2.用Python反转字符串的最佳方法 (2. Best Way to Reverse a String in Python)

We can reverse a string through multiple algorithms. We have already seen six of them. But which of them you should choose to reverse a string.

我们可以通过多种算法反转字符串。 我们已经看过其中的六个。 但是您应该选择其中的哪一个反向字符串。

We can use timeit module to run multiple iterations of these functions and get the average time required to run them.

我们可以使用timeit模块来运行这些函数的多次迭代,并获得运行它们所需的平均时间。

All the above functions are stored in a python script named string_reverse.py. I executed all these functions one by one for 1,00,000 times using the timeit module and got the average of the best 5 runs.

以上所有功能均存储在名为string_reverse.py的python脚本中。 我使用timeit模块一个接一个地执行了所有这些功能1,00,000次,并得到了最佳5次运行的平均值。

$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_slicing("ABç∂EF"*10)'
100000 loops, best of 5: 0.449 usec per loop

$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_list("ABç∂EF"*10)'
100000 loops, best of 5: 2.46 usec per loop

$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_join_reversed_iter("ABç∂EF"*10)'
100000 loops, best of 5: 2.49 usec per loop

$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_for_loop("ABç∂EF"*10)'
100000 loops, best of 5: 5.5 usec per loop

$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_while_loop("ABç∂EF"*10)'
100000 loops, best of 5: 9.4 usec per loop

$ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_recursion("ABç∂EF"*10)'
100000 loops, best of 5: 24.3 usec per loop

The below table presents the results and slowness of an algorithm from the best one.

下表列出了最佳算法的结果和慢度。

AlgorithmTimeIt Execution Time (Best of 5)Slowness
Slicing0.449 usec1x
List reverse()2.46 usec5.48x
reversed() + join()2.49 usec5.55x
for loop5.5 usec12.25x
while loop9.4 usec20.94x
Recursion24.3 usec54.12x
算法 TimeIt执行时间(最佳5) 缓慢
切片 0.449微秒 1倍
列出reverse() 2.46微秒 5.48倍
reversed()+ join() 2.49微秒 5.55倍
for循环 5.5微秒 12.25倍
while循环 9.4用 20.94倍
递归 24.3微秒 54.12倍

3.总结 (3. Summary)

We should use slicing to reverse a string in Python. Its code is very simple and small and we don’t need to write our own logic to reverse the string. Also, it’s the fastest way to reverse a string as identified by the above test executions.

我们应该使用切片来反转Python中的字符串。 它的代码非常简单小巧,我们不需要编写自己的逻辑来反转字符串。 另外,这是反转上述测试执行所确定的字符串的最快方法。

GitHub Repository.
GitHub存储库中检出完整的python脚本和更多Python示例。

4.参考 (4. References)

翻译自: https://www.journaldev.com/23647/python-reverse-string

python字符串反向输出

    原文作者:cunchi4221
    原文地址: https://blog.csdn.net/cunchi4221/article/details/107475831
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞