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

相关文章

itchat接口使用示例

有关itchat接口的知识,小编是初步学习,这里先给大家分享一段代码用法示例。 sudo pip3 install itchat 今天用了下itchat接口,从url=”https://...

python函数的作用域及关键字详解

函数的作用域 python中的作用域分4种情况: L:local,局部作用域,即函数中定义的变量; E:enclosing,嵌套的父级函数的局部作用域,即包含此函数的上级函数...

python2 与 python3 实现共存的方法

python2 与 python3 实现共存的方法

1.现在我本机系统已内置python2.6 2.下载进行源码安装 复制链接下载到/root/mypackage,解压 接着 mkdir /usr/local/python3 然后在解...

python初学者,用python实现基本的学生管理系统(python3)代码实例

这个是用python实现的基本的增删改查的学生管理系统吧,其中主要是对输入的数据进行合法性检测的问题,这次又对函数进行了练习!掌握函数更加熟练了!二话不说先贴代码,一切问题请看注释,都很...

Python3正则匹配re.split,re.finditer及re.findall函数用法详解

本文实例讲述了Python3正则匹配re.split,re.finditer及re.findall函数用法。分享给大家供大家参考,具体如下: re.split re.finditer r...