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

yipeiwu_com7年前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中的字符串反转的讲解,大家如果有疑问可以留言,或者到本站社区讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

Python ValueError: invalid literal for int() with base 10 实用解决方法

今天在写爬虫程序的时候由于要翻页,做除法分页的时候出现了 复制代码 代码如下: totalCount = '100' totalPage = int(totalCount)/20 Va...

用实例分析Python中method的参数传递过程

什么是method? function就是可以通过名字可以调用的一段代码,我们可以传参数进去,得到返回值。所有的参数都是明确的传递过去的。 method是function与对象的结合。我...

python目录与文件名操作例子

1、操作目录与文件名 #!/usr/bin/env python #-*- coding: utf-8 -*- import os,re import shutil import...

python中将阿拉伯数字转换成中文的实现代码

复制代码 代码如下: #!/usr/bin/python #-*- encoding: utf-8 -*- import types class NotIntegerError(Exce...

Python实现获取照片拍摄日期并重命名的方法

Python实现获取照片拍摄日期并重命名的方法

本文实例讲述了Python实现获取照片拍摄日期并重命名的方法。分享给大家供大家参考,具体如下: python获取照片的拍摄日期并重命名。不支持重复处理的中断。 重命名为:拍摄日期__原文...