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


相关推荐

  • 初探Protostuff的使用[通俗易懂]

    初探Protostuff的使用[通俗易懂]初探Protostuff的使用最近在学习RPC,看到了一个叫做Protostuff的库,是基于谷歌ProtocalBuffer的序列化库,之前了解过ProtocolBuffer,对学习了一些资料后,写了个demo,记录下来。什么是ProtocolBuffer?ProtocolBuffer是谷歌出品的一种数据交换格式,独立于语言和平台,类似于json。Google提供…

    2022年6月7日
    35
  • Linux 防火墙开放特定端口 (iptables)

    Linux 防火墙开放特定端口 (iptables)查看状态:iptables-L-n下面添加对特定端口开放的方法:使用iptables开放如下端口/sbin/iptables-IINPUT-ptcp–dport8000-jACCEPT保存/etc/rc.d/init.d/iptablessave重启服务serviceiptablesrestart查看需要打开的端口是否生效?/etc/init.d/iptablesstatus法2:或直接编辑/etc/sysconfig/iptables-AINPUT

    2022年10月19日
    1
  • pycharm激活码2021年csdn[在线序列号]

    pycharm激活码2021年csdn[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    49
  • Android系统各版本号及代号「建议收藏」

    Android系统各版本号及代号

    2022年1月18日
    90
  • PostgreSQL查询全角字符「建议收藏」

    PostgreSQL查询全角字符「建议收藏」PostgreSQL中~*的妙用PostgreSQL中可以使用~*进行模糊查询,同时会忽略大小写,在查询的条件中也会支持正则表达式。所以在查询全角字符的时候,可以使用正则表达式来来进行查询。select*from表名where字段~*'[A-Za-z0-9]’;使用上面SQL就可以查询出数据库中所有包含全角字符的数据了。…

    2025年6月5日
    1
  • 读写锁属性[通俗易懂]

    读写锁属性[通俗易懂]本文转载自zfy3000《读写锁属性》通过读写锁,可以对受保护的共享资源进行并发读取和独占写入。读写锁是可以在读取或写入模式下锁定的单一实体。要修改资源,线程必须首先获取互斥写锁。必须释放所有读锁之后,才允许使用互斥写锁。有关Solaris线程所实现的读写锁,请参见相似的同步函数-读写锁。对数据库的访问可以使用读写锁进行同步。读写锁支持并发读取数据库记录,因为读操作

    2022年8月12日
    7

发表回复

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

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