在 Python 中反转字符串 [英] Reverse a string in Python

查看:28
本文介绍了在 Python 中反转字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 的 str 对象没有内置的 reverse 函数.实现这种方法的最佳方式是什么?

如果提供非常简洁的答案,请详细说明其效率.比如str对象是否转换为不同的对象等

解决方案

怎么样:

<预><代码>>>>'你好世界'[::-1]'dlrow olleh'

这是扩展切片语法.它的工作原理是执行 [begin:end:step] - 通过离开开始和结束并指定 -1 的步骤,它反转一个字符串.

There is no built in reverse function for Python's str object. What is the best way of implementing this method?

If supplying a very concise answer, please elaborate on its efficiency. For example, whether the str object is converted to a different object, etc.

解决方案

How about:

>>> 'hello world'[::-1]
'dlrow olleh'

This is extended slice syntax. It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string.

这篇关于在 Python 中反转字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆