题目: 在Python环境下用尽可能多的方法反转字符串,例如将s = "abcdef"反转成 “fedcba”
result = s[::-1]
l = list(s)
l.reverse()
result = "".join(l)
当然下面也行
l = list(s)
result = "".join(l[::-1])
result = reduce(lambda x,y:y+x,s)
def func(s):
if len(s) <1:
return s
return func(s[1:])+s[0]
result = func(s)
def func(s):
l = list(s) #模拟全部入栈
result = ""
while len(l)>0:
result += l.pop() #模拟出栈
return result
result = func(s)
def func(s):
result = ""
max_index = len(s)-1
for index,value in enumerate(s):
result += s[max_index-index]
return result
result = func(s)
判断一个数是否为回文数(正向读和反向读结果一样)
num = input('请输入要判断是否为回文数的数:')
if (num[::-1] == num[:]):
print ('%s为回文数'%num)
else:
print ('%s不是回文数'%num )
ent=input("请输入一句话: ")
a=ent[0::1]
b=ent[-1::-1]
if a==b:
print("回数")
else:
print("不是回数")