python学习笔记:翻转字符串

使用s[ : : -1]即可,但最好的方式写自定义函数,清楚明了

s = "abcdefgh"

print("This works, but is confusing:")
print(s[::-1])

print("This also works, but is still confusing:")
print("".join(reversed(s)))

print("Best way: write your own reverseString() function:")

def reverseString(s):
    return s[::-1]

print(reverseString(s)) # crystal clear!

猜你喜欢

转载自blog.csdn.net/xiaozhimonica/article/details/85317212