Python 反转字符串_python输出字符串

Python 反转字符串_python输出字符串python字符串反转方法Helloeveryone,inthistutorialwe’llseedifferentwaystoreversestringinPython.大家好,在本教程中,我们将看到在Python中反转字符串的不同方法。Asweknow,wecanreversealistusingreverse()methodbutPy…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

Python 反转字符串_python输出字符串

python字符串反转方法

Hello everyone, in this tutorial we’ll see different ways to reverse string in Python.

大家好,在本教程中,我们将看到在Python中反转字符串的不同方法。

As we know, we can reverse a list using reverse() method but Python doesn’t have the reverse() method for string.

众所周知,我们可以使用reverse()方法来反转 列表,但是Python没有用于字符串reverse()方法

Here are some alternate and easy ways to reverse a string.

这是一些可逆的简单方法。

在Python中反转字符串的方法 (Ways to Reverse String in Python)

1.使用循环 (1. Using Loop)

string1 =  "the crazy programmer"
string2 = ""
 
i = len(string1)-1
 
while(i>=0):
  string2 = string2 + string1[i]
  i = i-1
 
print "original = " + string1
print "reverse  = " + string2

Jetbrains全家桶1年46,售后保障稳定

Output:

输出:

original = the crazy programmer reverse = remmargorp yzarc eht

原始=疯狂的程序员 反向= remmargorp yzarc等

In above program, we’ve started a loop from the last index (length-1) to first index (0) of string1. In each step of loop, it will pick the character from right-side in string1 and concatenate with string2.

在上面的程序中,我们开始了从string1的最后一个索引(length-1)到第一个索引(0)的循环。 在循环的每个步骤中,它将从string1的右侧选择字符并与string2连接。

2.使用递归 (2. Using Recursion)

def reverse_it(string):
  if len(string)==0:
    return string
  else:
    return reverse_it(string[1:]) + string[0]
    print "added " + string[0]
 
string1 = "the crazy programmer"
string2 = reverse_it(string1)
 
print "original = " + string1
print "reversed = " + string2

In above program, there is a reverse_it() method which accepts a string and then it will check if the string is empty or not, if empty then it will return the string otherwise it will call itself by passing string from its second character to last character.

在上面的程序中,有一个reverse_it()方法接受一个字符串,然后它将检查该字符串是否为空,如果为空,则将返回该字符串,否则它将通过将字符串从第二个字符传递到最后一个字符来进行调用字符。

String = “hello”

字符串=“你好”

Print string[1:]

打印字符串[1:]

Output:  ‘ello’

输出:“ ello”

After calling reverse_it() method again and again there will be a point when string will be empty then the condition

一次又一次地调用reverse_it()方法之后,将有一个字符串为空的条件,

if len(string) == 0:

如果len(string)== 0:

will be true , it will return the string.  return statement will throw the execution, where it came from.

将为true,它将返回字符串。 return语句将抛出执行源。

So in

所以在

Return reverse_it(string[1:])  +  string[0]

返回reverse_it(string [1:])+ string [0]

the “+ string[0] “ will be executed next, which will add the first letter at last.

接下来将执行“ + string [0]” ,最后将添加第一个字母。

3.使用堆栈 (3. Using Stack)

def create_stack():
  #it will  create a List named as stack and return it
  stack = []
  return stack
 
def push(stack,element):
  #it will add a new element to List
  stack.append(element)
 
def pop(stack):
  #it will delete the last element from  List
  if len(stack) == 0:
    return
  return stack.pop()
 
def reverse(string):
 
  #method to reverse the string using stack's functions
  n = len(string)
  
  #to create a empty list (stack)
  stack = create_stack()
 
  #inserting character of string into List
  for i in range(0,n):
    push(stack,string[i])
 
  #making string empty
  string = ""
 
  #getting last element of the List (stack) and storing it into string
  for i in range(0,n):
    string = string + pop(stack)
  return string
 
string1 = "the crazy programer"
string2 = reverse(string1)
 
print "original = " + string1
print "reversed = " + string2

In above program, we’re using concept of stack having push and pop functions.

在上面的程序中,我们使用具有推入和弹出功能的堆栈概念。

To implement stack concept we’re using list.

为了实现堆栈概念,我们使用列表。

When we call reverse() method, it will create a list named as ‘stack’ and insert all the characters of string into list using push() method. At last it will fetch all the elements in the list from last to first one by one and store them into the string.

当我们调用reverse()方法,它会创建一个使用push()方法命名为“叠加”和插入的所有字符串的字符到列表清单 。 最后,它将从上到下依次提取列表中的所有元素,并将它们存储在字符串中。

4.使用扩展切片 (4. Using Extended Slice)

string = "the crazy programmer"
print "original = " + string
 
string = string[::-1]
print "reversed = " + string

Mostly extended slice is used for skipping the steps but if we put -1 in third ‘step’ or ‘stride’ argument then we can get the reverse of a string, list and tupple.

大多数情况下,扩展切片用于跳过步骤,但是如果我们在第三个“ step”“ stride”参数中输入-1,则可以得到字符串,列表和tupple的倒序。

5.使用清单 (5. Using List)

string = "the crazy programmer"
print "original = " + string
 
#convrting string into list
list1 = list(string)
 
#applying reverse method of list
list1.reverse()
 
#converting list into string
string = ''.join(list1)
print "reversed = " + string

String doesn’t have reverse() method but lists have. So we are converting string into list, performing reverse() operation and again converting it back into string using ‘ ’.join() method.

字符串没有reverse()方法,但列表具有。 因此,我们将字符串转换为列表,执行reverse()操作,然后再次使用”.join()方法将其转换回字符串。

Comment below if you have queries or know any other way to reverse a string in python.

如果您有疑问或知道以其他方式在python中反转字符串,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/12/reverse-string-python.html

python字符串反转方法

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/206872.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 损失函数与代价函数区别

    损失函数与代价函数区别各种损失函数的优缺点详解损失函数或者代价函数的目的是:衡量模型的预测能力的好坏。损失函数(Lossfunction):是定义在单个训练样本上的,也就是就算一个样本的误差,比如我们想要分类,就是预测的类别和实际类别的区别,是一个样本的哦,用L表示。代价函数(Costfunction):是定义在整个训练集上面的,也就是所有样本的误差的总和的平均,也就是损失函数的总和的平均,有没有这个平…

    2022年5月12日
    62
  • pip安装scrapy失败_scrapy框架运行

    pip安装scrapy失败_scrapy框架运行错误如图所示,running setup.py install for Twisted…..errorTwisted依赖库安装报错,重新下载手动安装一下下载网址:https://www.lfd.uci.edu/~gohlke/pythonlibs注意:看下安装的python是什么版本,我安装的python 3.9.0,就下载cp39,64位的下载安装的版本不对,就会报:Twisted-20.3.0-cp38-cp38-win_amd64.whl is not a support…

    2022年8月18日
    8
  • 基于JAVA+Servlet+JSP+MYSQL的图书销售管理系统

    基于JAVA+Servlet+JSP+MYSQL的图书销售管理系统项目功能:此网上书店系统具有以下基本功能:1.用户注册功能:进入网上书店的用户可以进行商品浏览,但不能进行购买,此时用户的身份为游客。如需购买图书,就要用到用户注册功能。需要输入用户名和密码进行注册。如果已注册的用户忘记密码,可以点击“找回密码”按钮。已注册用户也可以点击“注销”按钮进行用户信息注销。2.商品管理功能:商品管理功能即用户可以对网上书店的书籍进行搜索、查看、选购。在管理员方面,此功能还包括系统内图书的上新、下架管理。3.书店购物车功能:用户可以将心仪的图书加入到书店购物车中。在书店购物

    2022年5月18日
    45
  • 900万!!!!!!!!这也太强了吧!!!我的老天!!!!!!!!!!

    900万!!!!!!!!这也太强了吧!!!我的老天!!!!!!!!!!大家好,我是二哥呀!之前在送书的时候做了一个小调查,问题是:“你是怎么认识二哥的?”我以为从知乎上了解的多一些,没想到,CSDN上的最多,看来二哥还是在CSDN上更有影响力一些,这个结果多少让我感到有些意外,因为我最近在知乎上更新得更勤快一些。写这篇文章的时候,我去CSDN上看了一眼我的主页。访问量突破了900万!按照目前的增长速度来看,年底突破1000万访问量应该没啥大问题。另外还有一些数据我觉得也挺牛逼的:原创文章数量957篇;作者总榜第12名;作者周榜第

    2022年6月7日
    27
  • Linux中查看cuda版本

    Linux中查看cuda版本安装torch时,需要查看cuda版本,可以用如下命令查看:nvcc-V

    2022年5月2日
    57
  • RST报文详解_modbus网关使用方法

    RST报文详解_modbus网关使用方法我们知道TCP建立连接的时候需要三次连接,TCP释放连接的时候需要四次挥手,在这个过程中,出现了很多特殊的标志报文段,例如SYNACKFIN,在TCP协议中,除了上面说了那些标志报文段之外,还有其他的报文段,如PUSH标志报文段以及今天需要重点讲解的RST报文段。RST:(Resettheconnection)用于复位因某种原因引起出现的错误连接,也用来拒绝非法数据和请求。如果接收到R…

    2022年10月1日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号