当前位置:柔美女性网 >

绿色生活 >心理 >

python里面怎么把字符串从后往前读

python里面怎么把字符串从后往前读

python里面怎么把字符串从后往前读

python的str对象中没有内置的反转函数

python中,字符换是不可变,更改字符串不会修改字符串,而是创建一个新的字符串。

如 a=�' 反转成 a=�'

第一种方法:使用字符串切片

>>> a=�'

>>> a = a[::-1]

�'

第二种方法:使用reversed() 可读行好,但速度较慢

>>> '&#(reversed(�'))

�'

  • 文章版权属于文章作者所有,转载请注明 https://rmnxw.com/lvse/xinli/lpm9p3.html