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


相关推荐

  • Vue(10)表单输入绑定v-model「建议收藏」

    Vue(10)表单输入绑定v-model「建议收藏」v-modelv-model指定可以实现表单值与属性的双向绑定。即表单元素中更改了值会自动的更新属性中的值,属性中的值更新了会自动更新表单中的值绑定的属性和事件v-model在内部为不同的输入元

    2022年7月29日
    6
  • linux防ddos 软件下载,linux系统防ddos攻击工具

    linux防ddos 软件下载,linux系统防ddos攻击工具DDoSdeflate其实非常简单,下面是详细的安装和配置步骤:1.安装DDoSdeflatewgethttp://www.inetbase.com/scripts/ddos/install.shchmod0700install.sh./install.sh//弹出版权信息后,在SSH下键入q,即可退出版权信息。2.配置DDoSdeflate.安装成功后会提示Config…

    2022年7月14日
    11
  • settings官方网站_phpstorm中文

    settings官方网站_phpstorm中文setting —> php,选择php版本,并点击…,选择到php.exe进入到appserv底下找到php.ini文件,查找date.timezone,去掉前面的;号,添加”Asia/Shanghai”重启appserv环境,就是重启下apache 和 mysql服务发现依然失败后面重启电脑就可以了哈哈哈哈哈哈哈哈…

    2022年8月18日
    5
  • dropdownlist事件的用法_list down

    dropdownlist事件的用法_list down前台添加了DropDownList以后,ListItem设置完成以后,想添加事件SelectedIndexChanged,如果没有在前台设置属性AutoPostBack=”true”,事件是不能触发的.下面是我修改成功的例子:前台代码:                                                                    

    2022年9月12日
    0
  • zuul 网关的用途_Zuul网关

    zuul 网关的用途_Zuul网关Zuul的主要作用:1.路由,进行请求转发。2.鉴权,进行身份认证(安全)。3.限流,限制访问的数量,保证服务器的稳定。除此之外还有以下作用4.负载均衡。5.压力测试。6.监控1.Zuul简单使用1.添加依赖org.springframework.cloudspring-cloud-starter-netflix-zuul2.添加@EnableZuulProxy注解@EnableZuulProx…

    2022年8月15日
    6
  • mediumtext_mysql数据类型介绍(含text,longtext,mediumtext说明) | 学步园[通俗易懂]

    mediumtext_mysql数据类型介绍(含text,longtext,mediumtext说明) | 学步园[通俗易懂]由MySQL支持的列类型列在下面。下列代码字母用于描述中:M指出最大的显示尺寸。最大的合法的显示尺寸是255。D适用于浮点类型并且指出跟随在十进制小数点后的数码的数量。最大可能的值是30,但是应该不大于M-2。方括号(“[”和“]”)指出可选的类型修饰符的部分。注意,如果你指定一个了为ZEROFILL,MySQL将为该列自动地增加UNSIGNED属性。TINYINT[(M)][UNSIG…

    2022年5月5日
    598

发表回复

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

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