python字符串的翻转实现的两种方法
python字符串的翻转实现的两种方法
方法一:利用切片
str1="helloworld!"
print(str1[::-1])
方法二:利用reduce函数实现
fromfunctoolsimportreduce
str1="helloworld!"
print(reduce(lambdax,y:y+x,str1))
补充:判断字符串是不是回文串
str1="123455"
deffun(string):
print("%s"%string==string[::-1]and"YES"or"NO")
if__name__=='__main__':
fun(str1)
以上就是python字符串的翻转实现的两种方法,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python中静态字符串的编码
python中静态字符串的编码1、静态字符串的编码方法取决于报头中的编码设置。在下面的代码中,变量名用UTF-8编码。#!/usr/bin/envpython#coding:...详情>>
2023-11-13 23:57:40
pythonpyg2plot的原理
pythonpyg2plot的原理1、说明PyG2Plot原理其实非常简单,其中借鉴了pyecharts的实现,但是因为蚂蚁金服的G2Plot完全基于可视分析理论的配置式结...详情>>
2023-11-13 21:25:26
python继承的特征有哪些?
python继承的特征有哪些?本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、概念继承就是一个类可以获得另外一个类中的成员属性和...详情>>
2023-11-13 19:47:58
pythonFileNotFoundError的异常
python中FileNotFoundError的异常1、Python无法读取不存在的文件,因此它引发一个异常:Traceback(mostrecentcalllast):File"alice.py"...详情>>
2023-11-13 19:27:04热门推荐
技术干货






