Python如何借助Swap操作逆转字符串?

1 个回答

Coco老师 - 科普专家
擅长:编程教育
#!usr/bin/env python
#encoding:utf-8
   
def strReverse(strDemo):
  strList=list(strDemo)
  if len(strList)==0 or len(strList)==1:
    return strList
  i=0
  length=len(strList)
  while i < length/2:
    strList[i], strList[length-i-1]=strList[length-i-1], strList[i]
    i+=1
  return ''.join(strList)
print(strReverse('qbitschool.cn'))