详解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使用Flask-SQLAlchemy连接数据库操作示例

本文实例讲述了Python使用Flask-SQLAlchemy连接数据库操作。分享给大家供大家参考,具体如下: 需要安装flask pip install flask 安装My...

python3 kmp 字符串匹配的方法

python3 kmp 字符串匹配的方法

先声明,本人菜鸟一个,写博客是为了记录学习的过程,以及自己的理解和心得,可能有的地方写的不好,希望大神指出。。。 抛出问题 给定一个文本串test_str(被匹配的字符串)和模式串pat...

python将pandas datarame保存为txt文件的实例

CSV means Comma Separated Values. It is plain text (ansi). The CSV ("Comma Separated Value")...

Python time库基本使用方法分析

本文实例讲述了Python time库基本使用方法。分享给大家供大家参考,具体如下: 时间获取 time() 获取当前时间戳,为一个浮点数 >>>time....

python+opencv实现霍夫变换检测直线

python+opencv实现霍夫变换检测直线

本文实例为大家分享了python+opencv实现霍夫变换检测直线的具体代码,供大家参考,具体内容如下 python+opencv实现高斯平滑滤波 python+opencv实现阈值分...