Python字符串逆序的实现方法【一题多解】

yipeiwu_com6年前Python基础

/post/156406.htm
/post/156407.htm

1. 使用索引

>> strA = 'abcdefg'
>> strA[::-1]
'gfedcba'

2. 使用 list 的 reverse 方法

>> l = [c for c in strA]
>> l.reverse()
>> ''.join(l)
'gfedcba'

3. 使用 python 原生函数:reversed

>> ''.join(c for c in reversed(strA))
'gfedcba'

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【听图阁-专注于Python设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

PYQT5设置textEdit自动滚屏的方法

在修改后的文字后面加上: self.textEdit_6.moveCursor(QTextCursor.End) 例子: self.textEdit_6.setPlainTe...

Python从文件中读取指定的行以及在文件指定位置写入

Python从文件中读取指定的行 如果想根据给出的行号, 从文本文件中读取一行数据,  Python标准库linecache模块非常适合这个任务: 测试文件内容 : This...

python3实现字符串操作的实例代码

python3字符串操作 x = 'abc' y = 'defgh' print(x + y) #x+y print(x * 3) #x*n print(x...

python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法

python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法

1. test.txt文件,数据以逗号分割,第一个数据为x坐标,第二个为y坐标,数据如下:1.1,2 2.1,2 3.1,3 4.1,5 40,38 42,41 43,42 2....

Python 字典dict使用介绍

Python字典的创建 方法一: >>> blank_dict = {} >>> product_dict = {'MAC':8000,'Ipho...