python对指定字符串逆序的6种方法

简介: python对指定字符串逆序的6种方法

对于一个给定的字符串,逆序输出,这个任务对于python来说是一种很简单的操作,毕竟强大的列表和字符串处理的一些列函数足以应付这些问题 了,今天总结了一下python中对于字符串的逆序输出的几种常用的方法


方法一:直接使用字符串切片功能逆转字符串

#!usr/bin/env python 
# encoding:utf-8 
def strReverse(strDemo):
  return strDemo[::-1] 
print(strReverse('pythontab.com'))


结果:

moc.batnohtyp

方法二:遍历构造列表法

循环遍历字符串, 构造列表,从后往前添加元素, 最后把列表变为字符串

#!usr/bin/env python 
# encoding:utf-8 
def strReverse(strDemo): 
  strList=[] 
  for i in range(len(strDemo)-1, -1, -1): 
    strList.append(strDemo[i])
  return ''.join(strList)
print(strReverse('pythontab.com'))

结果:

1

moc.batnohtyp


方法三:使用reverse函数

将字符串转换为列表使用reverse函数

#!usr/bin/env python 
# encoding:utf-8 
def strReverse(strDemo): 
  strList = list(strDemo) 
  strList.reverse() 
  return ''.join(strList)
print(strReverse('pythontab.com'))

结果:

moc.batnohtyp

方法四:借助collections模块方法extendleft

#!usr/bin/env python 
# encoding:utf-8 
import collections 
def strReverse(strDemo): 
  deque1=collections.deque(strDemo) 
  deque2=collections.deque() 
  for tmpChar in deque1: 
    deque2.extendleft(tmpChar) 
  return ''.join(deque2) 
print(strReverse('pythontab.com'))


结果:

moc.batnohtyp

方法五:递归实现

#!usr/bin/env python 
# encoding:utf-8 
def strReverse(strDemo): 
  if len(strDemo)<=1: 
    return strDemo 
  return strDemo[-1]+strReverse(strDemo[:-1]) 
print(strReverse('pythontab.com'))


结果:

moc.batnohtyp

方法六:借助基本的Swap操作,以中间为基准交换对称位置的字符

#!usr/bin/env python 
#encoding:utf-8 
def strReverse(strDemo): 
  strList=list(strDemo) 
  if len(strList)==0 or len(strList)==1: 
    return strList 
  i=0 
  length=len(strList) 
  while i < length/2: 
    strList[i], strList[length-i-1]=strList[length-i-1], strList[i] 
    i+=1
  return ''.join(strList)
print(strReverse('pythontab.com'))

结果:

moc.batnohtyp
















相关文章
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
697 1
|
机器学习/深度学习 数据采集 数据挖掘
基于 GARCH -LSTM 模型的混合方法进行时间序列预测研究(Python代码实现)
基于 GARCH -LSTM 模型的混合方法进行时间序列预测研究(Python代码实现)
449 2
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
620 100
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
706 99
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
12月前
|
人工智能 数据安全/隐私保护 异构计算
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
1767 8
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
|
11月前
|
存储 Java 索引
(Python基础)新时代语言!一起学习Python吧!(二):字符编码由来;Python字符串、字符串格式化;list集合和tuple元组区别
字符编码 我们要清楚,计算机最开始的表达都是由二进制而来 我们要想通过二进制来表示我们熟知的字符看看以下的变化 例如: 1 的二进制编码为 0000 0001 我们通过A这个字符,让其在计算机内部存储(现如今,A 字符在地址通常表示为65) 现在拿A举例: 在计算机内部 A字符,它本身表示为 65这个数,在计算机底层会转为二进制码 也意味着A字符在底层表示为 1000001 通过这样的字符表示进行转换,逐步发展为拥有127个字符的编码存储到计算机中,这个编码表也被称为ASCII编码。 但随时代变迁,ASCII编码逐渐暴露短板,全球有上百种语言,光是ASCII编码并不能够满足需求
452 4

推荐镜像

更多