Dom*_*ger 0 python string python-3.x
myString = "test (with (w)ords) swap"
Run Code Online (Sandbox Code Playgroud)
反转字符串并交换(with)和)with(
输出: "paws (sdro(w) htiw) tset"
有一个简单的方法吗?
您可以通过切片和(对于Python 3)使用str.translate适当的str.maketrans映射来反转字符串:
>>> s = "test (with (w)ords) swap"
>>> s[::-1].translate(str.maketrans('()', ')('))
'paws (sdro(w) htiw) tset'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62 次 |
| 最近记录: |