Python implements a method to reverse output a string of a specified input

Source: Internet
Author: User
This article mainly describes the python implementation of the specified input string reverse output of 6 methods, with a certain reference value, interested in small partners can refer to

For a given string, reverse output, this task is a very simple operation for Python, after all, powerful list and string processing of some of the column functions enough to deal with these problems, and today summed up in Python in the inverse of the string output of several common methods, Summed up a total of six methods, personally think more comprehensive, if there are other methods welcome to supplement

method One: use the string slicing function directly to reverse a string

method Two: convert a string to a list using the reverse function

method Three: create a new list and add elements from the forward

method Four: with the help of the collections module, the ready-made method Extendleft

method Five: recursive implementation

method Six: Exchange the character of the symmetric position with the intermediate reference with the basic swap operation

Here are the specific implementations:

#!usr/bin/env python #encoding: Utf-8 "" __author__: Yishui Cold City Function: Enter a string, flashback output ' Import collections def func1 (one_str : "' Use string slicing function to reverse string ' ' return one_str[::-1] def func2 (one_str):" ' Convert string to list using reverse function ' One_str_li st = List (ONE_STR) One_str_list.reverse () return ". Join (One_str_list) def func3 (one_str):" ' Create a new list, add elements from the back " ' one_list=[] for I in range (len (ONE_STR) -1,-1,-1): One_list.append (One_str[i]) return '. Join (one_list) def FUNC4 (  ONE_STR): "" Collections Module-ready Method Extendleft ' Deque1=collections.deque (ONE_STR) Deque2=collections.deque () for One_char in Deque1:deque2.extendleft (One_char) return '. Join (DEQUE2) def func5 (one_str): "' recursive implementation ' if Len (ONE_STR) <=1:return one_str return One_str[-1]+func5 (One_str[:-1]) def func6 (ONE_STR): "" with basic swap operation, centered on the middle Exchange symmetric position of the character ' ' One_str_list=list (ONE_STR) If Len (one_str_list) ==0 or Len (one_str_list) ==1:return one_str_list i=0 L Ength=len (One_str_list) While I < length/2: One_str_list[i], one_str_list[length-i-1]=one_str_list[length-i-1], One_str_list[i] i+=1 Retu   RN ". Join (One_str_list) def main_func (str_list):" ' "" Keynote with Function "' for ONE_STR in str_list:one_list=[] one=[] One_list.append (Func1 (ONE_STR)) One_list.append (Func2 (ONE_STR)) One_list.append (func3 (ONE_STR)) One_list.append (   Func4 (ONE_STR)) One_list.append (Func5 (ONE_STR)) One_list.append (Func6 (ONE_STR)) print ' string {0} reverse order: '. Format (ONE_STR) Print One_list if __name__ = = ' __main__ ': str_list=[' 123456 ', ' abcdefg ', ' zyxvuw ', ' Together_cz '] main_func (str_list)

The results are as follows:

String 123456 reverse order: [' 654321 ', ' 654321 ', ' 654321 ', ' 654321 ', ' 654321 ', ' 654321 '] string ABCDEFG in reverse order: [' GFEDCBA ', ' GFEDCBA ', ' GFEDCBA ', ' gfedcba ', ' gfedcba ', ' GFEDCBA '] string Zyxvuw in reverse order: [' wuvxyz ', ' wuvxyz ', ' wuvxyz ', ' wuvxyz ', ' wuvxyz ', ' wuvxyz '] string TOGETHER_CZ reverse order: [' zc_rehtegot ', ' zc_rehtegot ', ' zc_rehtegot ', ' zc_rehtegot ', ' zc_rehtegot ', ' Zc_rehtegot ']

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.