-
python3教程之python对指定字符串逆序的6种方法
本站最新发布 Python从入门到精通|Python基础教程
试听地址 https://www.xin3721.com/eschool/python.html
试听地址 https://www.xin3721.com/eschool/python.html
对于一个给定的字符串,逆序输出,这个任务对于python来说是一种很简单的操作,毕竟强大的列表和字符串处理的一些列函数足以应付这些问题 了,今天总结了一下python中对于字符串的逆序输出的几种常用的方法
方法一:直接使用字符串切片功能逆转字符串
1
2
3
4
5
|
#!usr/bin/env python # encoding:utf-8 def strReverse(strDemo): return strDemo[:: - 1 ] print (strReverse( 'pythontab.com' )) |
结果:
1
|
moc.batnohtyp |
方法二:遍历构造列表法
循环遍历字符串, 构造列表,从后往前添加元素, 最后把列表变为字符串
1
2
3
4
5
6
7
8
|
#!usr/bin/env python # encoding:utf-8 def strReverse(strDemo): strList = [] for i in range ( len (strDemo) - 1 , - 1 , - 1 ): strList.append(strDemo[i]) return ''.join(strList) print (strReverse( 'pythontab.com' )) |
结果:
1
|
moc.batnohtyp |
方法三:使用reverse函数
将字符串转换为列表使用reverse函数
1
2
3
4
5
6
7
|
#!usr/bin/env python # encoding:utf-8 def strReverse(strDemo): strList = list (strDemo) strList.reverse() return ''.join(strList) print (strReverse( 'pythontab.com' )) |
结果:
1
|
moc.batnohtyp |
栏目列表
最新更新
python数据库连接池技术总结
python数据库连接池技术总结
成人网站性能提升 20 倍之经验谈 [Python
python动态捕获异常
python 探测网站目录的GUI程序
python实现中文字符繁体和简体中文转换
Python服务器开发 -- 网络基础
python高性能编程方法一
使用python管理Cisco设备
python抓取google搜索结果
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
图解TCP数据报结构以及三次握手(非常详
TCP协议的粘包问题(数据的无边界性)
Excel数据导入到Sql server
SQL Server like 字段
SQL Server中的LEFT、RIGHT函数
sql server 安装出现需要sqlncli.msi文件,错误
SQL Server学习内容(一)
SQLServer执行大脚本文件时,提示“无法执
数据库敏捷版本控制之3个数据库策略
将select 转为json
SQL Server 创建索引(index)
GROUP BY中的WITH CUBE、WITH ROLLUP原理测试及