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
















相关文章
|
14天前
|
Python
python魔法方法如何应用
【4月更文挑战第12天】这个Python示例展示了类继承和方法重写。`Student`类继承自`Person`,并覆盖了`say_hello`方法。通过`super().__init__(name)`调用父类的`__init__`初始化`name`属性,`Student`添加了`age`属性,并在重写的`say_hello`中使用。创建`Student`实例`student`并调用其`say_hello`,输出定制的问候信息。
20 1
|
3天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
21 0
|
3天前
|
Python
Python 字符串格式化指南
本文介绍了Python中的三种字符串格式化方法:1) 使用 `%` 操作符,如 `%s` 和 `%d`;2) `str.format()` 方法,通过 `{}` 占位符插入变量;3) Python 3.6 引入的 f-strings,直接在字符串内嵌入变量。此外,还提到了高级用法,如格式控制(如指定小数位数)。这些方法有助于更有效地处理和格式化字符串输出。
5 0
|
8天前
|
存储 关系型数据库 MySQL
Python搭建代理IP池实现存储IP的方法
Python搭建代理IP池实现存储IP的方法
|
8天前
|
Python
Python动态IP代理防止被封的方法
Python动态IP代理防止被封的方法
|
8天前
|
数据采集 存储 安全
python检测代理ip是否可用的方法
python检测代理ip是否可用的方法
|
9天前
|
数据可视化 测试技术 Python
在Python和R中使用交叉验证方法提高模型性能
在Python和R中使用交叉验证方法提高模型性能
20 0
|
10天前
|
存储 监控 开发工具
对象存储OSS产品常见问题之python sdk中的append_object方法支持追加上传xls文件如何解决
对象存储OSS是基于互联网的数据存储服务模式,让用户可以安全、可靠地存储大量非结构化数据,如图片、音频、视频、文档等任意类型文件,并通过简单的基于HTTP/HTTPS协议的RESTful API接口进行访问和管理。本帖梳理了用户在实际使用中可能遇到的各种常见问题,涵盖了基础操作、性能优化、安全设置、费用管理、数据备份与恢复、跨区域同步、API接口调用等多个方面。
45 9
|
10天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
52 0
|
10天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
51 0