详解Golang 与python中的字符串反转

yipeiwu_com5年前Python基础

详解Golang 与python中的字符串反转

在go中,需要用rune来处理,因为涉及到中文或者一些字符ASCII编码大于255的。

func main() {

  fmt.Println(reverse("Golang python"))

}
func reverse(src string) string {
  dst := []rune(src)
  len := len(dst)
  var result []rune
  result = make([]rune, 0)
  for i := len - 1; i >= 0; i-- {
   result = append(result, dst[i])
  }
  return string(result)
}

而在python中,有几种方法,一个是list的操作,一个是系统的自带的函数,还有一个采用上面的遍历的方法

#方法1--------------------------------------
s = 'Golang python'
print (s[::-1])

#方法2--------------------------------------
s = 'Golang python'
l = list(s)
l.reverse()
print (''.join(l) )

#方法3--------------------------------------
s = 'Golang python'
str=[]
k=0
for i in s:
  str.append(s[len(s)-1-k])
  k=k+1
print (''.join(str) )

#方法4--------------------------------------
s = 'Golang python'
str=[]
for i in s:
  str.insert(0,i)
print (''.join(str) )

以上就是关于Golang 与python中的字符串反转的讲解,大家如果有疑问可以留言,或者到本站社区讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

关于Pytorch的MNIST数据集的预处理详解

关于Pytorch的MNIST数据集的预处理详解

关于Pytorch的MNIST数据集的预处理详解 MNIST的准确率达到99.7% 用于MNIST的卷积神经网络(CNN)的实现,具有各种技术,例如数据增强,丢失,伪随机化等。 操作系统...

Python 3中的yield from语法详解

前言 最近在捣鼓Autobahn,它有给出个例子是基于asyncio 的,想着说放到pypy3上跑跑看竟然就……失败了。 pip install asyncio直接报invalid sy...

Django框架中数据的连锁查询和限制返回数据的方法

连锁查询 通常我们需要同时进行过滤和排序查询的操作。 因此,你可以简单地写成这种“链式”的形式: >>> Publisher.objects.filter(coun...

Django 使用easy_thumbnails压缩上传的图片方法

easy_thumbnails:A powerful, yet easy to implement thumbnailing application for Django 1.4+ 安...

Python的迭代器和生成器

先说迭代器,对于string、list、dict、tuple等这类容器对象,使用for循环遍历是很方便的。在后台for语句对容器对象调用iter()函数,iter()是python的内置...