Home  >  Article  >  Backend Development  >  Python realizes outputting strings in reverse order

Python realizes outputting strings in reverse order

王林
王林Original
2020-05-07 14:42:268463browse

Python realizes outputting strings in reverse order

Method for string output in reverse order:

1. Method through index

>>> strA = "abcdegfgijlk"
>>> strA[::-1]
'kljigfgedcba'

2. Flip with the help of list

#coding=utf-8 
strA = raw_input("请输入需要翻转的字符串:")
order = [] 
for i in strA:
 order.append(i)
order.reverse()  #将列表反转
print ''.join(order)  #将list转换成字符串

Result:

请输入需要翻转的字符串:abcdeggsdd
ddsggedcba

Recommended tutorial: python tutorial

The above is the detailed content of Python realizes outputting strings in reverse order. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn