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

yipeiwu_com6年前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中可迭代(Iterable)、迭代器(Iterator)与生成器(Generator)的概念

前言 在Python中可迭代(Iterable)、迭代器(Iterator)和生成器(Generator)这几个概念是经常用到的,初学时对这几个概念也是经常混淆,现在是时候把这几个概念搞...

Python分支结构(switch)操作简介

Python当中并无switch语句,本文研究的主要是通过字典实现switch语句的功能,具体如下。 switch语句用于编写多分支结构的程序,类似与if….elif….else语句。...

python 移动图片到另外一个文件夹的实例

如下所示: # -*- coding:utf8 -*- import os import shutil import numpy as np import pandas as p...

Python实现的排列组合计算操作示例

Python实现的排列组合计算操作示例

本文实例讲述了Python实现的排列组合计算操作。分享给大家供大家参考,具体如下: 1. 调用 scipy 计算排列组合的具体数值 >> from scipy.spec...

python使用电子邮件模块smtplib的方法

python使用电子邮件模块smtplib的方法

Smptp类定义:smtplib.SMTP(host[,port[,local_hostname[,,timeout]]]),作为SMTP的构造函数,功能是与smtp服务器建立连接,在连...