Python exercise 022: Use recursive functions to reverse the string, python022

Source: Internet
Author: User

Python exercise 022: Use recursive functions to reverse the string, python022

[Python exercise 022]The input five characters are printed in reverse order using the recursive function call method.

---------------------------------------

Another recursive question! However, with the experience of [Python exercise question 021: Recursive Method for factorial], it is not difficult to draw a factorial Based on Huludao. The Code is as follows:

Str = input ('enter several characters: ') def f (x): if x =-1: return ''else: return str [x] + f (x-1) print (f (len (str)-1 ))

The output result is as follows:

Enter several characters: abcdefg
Gfedcba

What if recursive functions are not required? It's much simpler: first convert the input string into a list, sort it in reverse order, and then join () together to bring it to the table. The Code is as follows:

Str = list (input ('enter several characters: ') str. reverse () print (''. join (str ))

It may be that my current level is too low to understand the power of recursive functions. A long journey ......

 

++

Source: getting started with programming languages: 100 typical examples [Python]

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.