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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 自然语言处理中的Attention机制总结[通俗易懂]

    自然语言处理中的Attention机制总结[通俗易懂]    在面试的过程中被问到了attention,原来虽然其实已经实际用过attention了,也知道个大概原理是加权求和,但是对于加权的具体方法以及权值得分的计算并不是很清晰,面试答的一般,正好最近实习的地方

    2022年7月24日
    9
  • LoadImage()使用

    LoadImage()使用

    2021年12月31日
    43
  • TFS 2010 让安装更简单,也让VSS成为历史「建议收藏」

    TFS 2010 让安装更简单,也让VSS成为历史「建议收藏」      一转眼VS2010RC(ReleaseCandidate)版本已经发布一月多了,RTM(ReleaseToManufacturer)版本也快妥了,已经进入了最后的倒计时,只等4月12号发布了。TFS2010也将一起正式发布,相对于2005和2008,2010将是具有里程碑意义的一个版本,就像它总设计师BrianHarry在下面的Channel9采访中所描述的:“T

    2022年9月24日
    5
  • web 打印控件_JS插件

    web 打印控件_JS插件 平常浏览网页和文档的时候,随处可见打印两个字,有时候不小心点到,就会弹出一个打印的页面,如果连接了打印机,可以直接调用到打印机进行真实的打印。做为开发人员我们在网页开发过程中经常会有打印页面的需求,目前我正在做浏览器端采用JS方式实现打印这么一个功能,通过JS来实现的方法有很多,这里我分享一下我自已采用的方法,供大家参考。为了节约开发时间,我采用的是第三方打印软件“老牌打印控件WebPrinter”。新版现在已更名为“智睦云打印”,在原来的基础上增加了云打印机的支持,“智睦云打印”可以应用在本..

    2025年7月1日
    4
  • 深入剖析MSAA_MSA分析报告

    深入剖析MSAA_MSA分析报告本文打算对MSAA(Multisampleantialiasing)做一个深入的讲解,包括基本的原理、以及不同平台上的实现对比(主要是PC与Mobile)。为了对MSAA有个更好的理解,所以写下了

    2022年8月3日
    6
  • java正则表达式语法详解及其使用代码实例[通俗易懂]

    java正则表达式语法详解及其使用代码实例[通俗易懂]java正则表达式语法详解及其使用代码实例http://www.zuidaima.com/share/1835085544524800.htm

    2022年7月19日
    26

发表回复

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

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