python字符串反转方法

Given a string and we have to reverse it by using stack and by using reversed method in python.

给定一个字符串,我们必须使用堆栈和python中的反转方法来反转它。

1)通过使用堆栈反转字符串 (1) Reverse a string by using stack)

Procedure:

程序:

  1. First create an empty stack

    首先创建一个空栈

  2. Push each character one by one in stack

    将每个字符一一推送

  3. Pop each character one by one and put them back to the string

    逐个弹出每个字符,然后将其放回字符串

2)使用reversed()方法反转串 (2) Reverse a strung using reversed() method)

In this method, we will use reversed() method and iterate over the reversed iterator to get the reversed string.

在此方法中,我们将使用reversed()方法并在反向迭代器上进行迭代以获取反向字符串。

Python代码反转字符串 (Python code to reverse a string )

import sys
def push(element, size, stack):
'''
this function is used to push the elements
in the stack and it will return Error! message
if the stack is full and terminate the program.
'''
global top
if top >= size - 1:
print('Stack Overflow')
sys.exit()
else:
top += 1
stack[top] = element
def pop():
'''
this function is used to pop elements from
the stack and it will return Error! message
if the stack is empty and terminate the program.
'''
global top
if top < 0:
print('Stack Underflow')
sys.exit()
else:
element = stack[top]
print('%s' % element, end='')
top -= 1
def reverse_by_sort(string):
'''
This function is used to reverse any string
by reversed() method.
'''
string = list(string)
rev_str = ''
for i in reversed(string):
rev_str += i
return rev_str
if __name__=='__main__':
size = 11
stack = [0]*size
string = 'Includehelp'
top = -1
# Pushing value in the stack
push('I', 11, stack)
push('n', 11, stack)
push('c', 11, stack)
push('l', 11, stack)
push('u', 11, stack)
push('d', 11, stack)
push('e', 11, stack)
push('h', 11, stack)
push('e', 11, stack)
push('l', 11, stack)
push('p', 11, stack)
print('Original String = %s' % string)
print('\nUsing Stack')
# Popping values from stack and printing them
print('Reversed String = ',end='')
for i in stack:
pop()
print('\n\nUsing sort()')
print('Reversed string = %s' % reverse_by_sort(string))

Output

输出量

Original String = Includehelp
Using Stack
Reversed String = plehedulcnI
Using sort()
Reversed string = plehedulcnI

翻译自: https://www.includehelp.com/python/reverse-a-string-using-stack-and-reversed-method.aspx

python字符串反转方法

python字符串反转方法_Python程序使用堆栈和反转方法反转字符串相关推荐

  1. python清空字典保留变量方法_python学习day06--02字典增删差改以及字符串的一些方法...

    a = '123'b= '456'c= a + b #拼接 print(c)print('hello' * 2) #连续的重复的输出某一个字符串 c= ''.join([a, '123'])print ...

  2. python 画图 线标注_Python画图的这几种方法,你学会了吗

    点击上方"科技学堂"订阅最新科技教育信息 循环是计算机程序中最基础的控制方法,在Python学习中,熟练使用循环语句是非常重要的."Python轻松学"课程我们 ...

  3. python 退出自定义函数_python通过自定义异常,提前退出方法

    python退出的操作,搜索后都是return.exit()等 return:退出一个方法,并返回一个值 exit():退出python 想要实现的功能: 方法A中调用多个方法,方法B.方法C..., ...

  4. python 接收外部参数_python 接收处理外带的参数方法

    python 接收处理外带的参数方法 在执行python 代码的时候,有时候需要传递外面的参数进行处理 这个该怎么实现呢? 需要一个模块 from sys import argv 当然也可以直接只导入 ...

  5. python 多重列表去重_Python对列表去重的多种方法(四种方法)

    Python对列表去重的多种方法(四种方法) 无聊统计了下列表去重到底有多少种方法.下面小编给大家总结一下,具体内容详情如下: 开发中对数组.列表去重是非常常见的需求,对一个list中的id进行去重, ...

  6. python 字符串输入时间_Python input()函数:获取用户输入的字符串

    input() 函数用于向用户生成一条提示,然后获取用户输入的内容.由于 input() 函数总会将用户输入的内容放入字符串中,因此用户可以输入任何内容,input() 函数总是返回一个字符串. 例如 ...

  7. python 为什么用%格式化_Python中应该使用%还是format来格式化字符串?

    原标题:Python中应该使用%还是format来格式化字符串? Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字 ...

  8. python threading模块的方法_Python THREADING模块中的JOIN()方法深入理解

    看了oschina上的两个代码,受益匪浅.其中对join()方法不理解,看python官网文档的介绍: join([timeout]):等待直到进程结束.这将阻塞正在调用的线程,直到被调用join() ...

  9. python实时监控文件大小_python实现实时监控文件的方法

    在业务稳定性要求比较高的情况下,运维为能及时发现问题,有时需要对应用程序的日志进行实时分析,当符合某个条件时就立刻报警,而不是被动等待出问题后去解决,比如要监控nginx的$request_time和 ...

最新文章

  1. 1001 字符串“水”题(二进制,map,哈希)
  2. js 面向对象例子
  3. 【EntityFramework 6.1.3】个人理解与问题记录
  4. [原]sencha touch之表单(login demo)
  5. 使用TensorFlow.js的AI聊天机器人三:改进了文本中的情感检测
  6. UIKit框架之NSObject
  7. yii2搭建完美后台并实现rbac权限控制实例教程
  8. 《一个操作系统的实现》 ubuntu系统环境配置
  9. 编译linux内核成vmlinuz,Linux内核vmlinuz文件认识
  10. 如何在网上隐藏自己的IP地址(转)
  11. 微信红包封面,你真的领取到了吗?
  12. 如何更新TeamViewer电脑客户端?
  13. MyEclipse 安装教程
  14. linux中硬链接为什么不能跨分区
  15. 生成.bks格式文件
  16. 未成年人勿进 谨以献给1980~1990出生的人(五)
  17. Java中的statis用法
  18. 【Android开发经验】Android移动UI设计经验总结
  19. 阿里云1核1G内存1M宽带可以支持多少IP访问量?
  20. OPENGL简介---反走样

热门文章

  1. corssover linux运行无效,使用 CrossOver 在 Linux运行 Windows 软件(金测OK)
  2. mysql对称连接什么意思_对称加密与非对称加密的区别是什么
  3. 尤克里里怎么样_尤克里里和吉他区别?尤克里里与吉他相比有什么不可替代的优势...
  4. python,pytorch:读取,保存,显示图片
  5. nn.Dataparallel pytorch 平行计算的两种方法
  6. 在linux上执行.net Console apps
  7. 介绍一下再Apache下的Tomcat负载均衡的一些使用问题
  8. 逐行粒度的vuex源码分析
  9. 【东营seo】SEO发展下的大机遇
  10. 《C#多线程编程实战(原书第2版)》——3.2 在线程池中调用委托