脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - Python实现字符串反转的6种方法

Python实现字符串反转的6种方法

2023-10-13 10:41Python程序u猿 Python

本文主要为大家介绍Python实现字符串反转的6种方法,有需要的朋友可以了解下

1.使用字符串切片
>>> s = "python"
>>> s[::-1]
'nohtyp'
>>>
2.使用列表的reverse方法
>>> s = "python"
>>> lst = list(s)
>>> lst.reverse()
>>> "".join(lst)
'nohtyp'
>>>

手写 reverse

>>> def reverseString(s:str) -> str:
    lst = list(s)
    i, j = 0, len(s)-1
    while i < j:
        lst[i], lst[j] = lst[j], lst[i]
        i , j = i + 1, j - 1
    return "".join(lst)
 
>>> s = 'python'
>>> reverseString(s)
'nohtyp'
>>>
3.使用reduce
>>> from functools import reduce  # Python3 中不可以直接调用reduce
>>> s = "python"
>>> reduce(lambda x, y: y+x, s)
'nohtyp'
>>>

reduce 函数帮助:

>>> help(reduce)
Help on built-in function reduce in module _functools:
 
reduce(...)
    reduce(function, sequence[, initial]) -> value
     
    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.
 
>>>
4.使用递归函数
>>> def reverse(s):
    if s == "":
        return s
    else:
        return reverse(s[1:]) + s[0]
 
     
>>> reverse('python')
'nohtyp'
>>>

python中默认的最大递归数:

>>> import sys
>>> sys.getrecursionlimit()
1000
>>>
5.使用栈
>>> def rev(s):
    lst = list(s) # 转换成list
    ret = ""
    while len(lst):
        ret += lst.pop() # 每次弹出最后的元素
    return ret
#Python小白学习交流群:711312441 
>>> s = 'python'
>>> rev(s)
'nohtyp'
>>>
6.for循环
>>> def rever(s):
    ret = ""
    for i in range(len(s)-1, 0, -1):
        ret += s[i]
    return ret
 
>>> s = "python"
>>> rev(s)
'nohtyp'
>>> 

到此这篇关于Python实现字符串反转的6种方法的文章就介绍到这了,更多相关内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/Python_222/article/details/130108777

延伸 · 阅读

精彩推荐
  • Pythonpython实现最长公共子序列

    python实现最长公共子序列

    这篇文章主要为大家详细介绍了python实现最长公共子序列的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    littlethunder8802021-02-23
  • PythonPython中的choice()方法使用详解

    Python中的choice()方法使用详解

    这篇文章主要介绍了Python中的choice()方法使用详解,是Python入门中的基础知识,需要的朋友可以参考下 ...

    Python教程网17072020-06-29
  • PythonPython学习笔记之线程

    Python学习笔记之线程

    这篇文章主要介绍了Python线程详解,本文详细讲解了线程方方面面的知识,如线程基础知识线程状态、线程同步(锁)、线程通信(条件变量)等内容,需要的...

    东山絮柳仔11822022-02-26
  • Pythonpython办公自动化之excel的操作

    python办公自动化之excel的操作

    在我们日常工作中,经常会使用 Word、Excel、PPT、PDF 等办公软件但是,经常会遇到一些重复繁琐的事情,这时候手工操作显得效率极其低下;通过...

    AirPython11162021-11-10
  • PythonPython实现解析Bit Torrent种子文件内容的方法

    Python实现解析Bit Torrent种子文件内容的方法

    这篇文章主要介绍了Python实现解析Bit Torrent种子文件内容的方法,结合实例形式分析了Python针对Torrent文件的读取与解析相关操作技巧与注意事项,需要的朋友...

    NickWar7472020-12-04
  • Python使用graphics.py实现2048小游戏

    使用graphics.py实现2048小游戏

    本文给大家分享的是使用Python实现2048小游戏的源码,非QT实现的哦,推荐给大家,有需要的小伙伴参考下吧。 ...

    hebedich4332019-11-25
  • PythonPython实现拷贝多个文件到同一目录的方法

    Python实现拷贝多个文件到同一目录的方法

    这篇文章主要介绍了Python实现拷贝多个文件到同一目录的方法,涉及Python针对文件与目录的遍历、复制等相关操作技巧,需要的朋友可以参考下...

    RQSLT11052020-09-08
  • PythonPython数学建模StatsModels统计回归模型数据的准备

    Python数学建模StatsModels统计回归模型数据的准备

    这篇文章主要介绍了Python数学建模StatsModels统计回归模型数据的准备学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步...

    youcans11532022-02-10