Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:29:30+00:00 2026-06-02T06:29:30+00:00

def function(s): if len(s) == 1: print s[0], else: function(s[1:]) print s[0], function(1234) ends

  • 0
def function(s):
 if len(s) == 1:
    print s[0],
 else:
    function(s[1:])
    print s[0],

function("1234") ends up printing 4 3 2 1

Why does this happen? In the function, obviously the first condition is not met. In the else condition, s[1:] is put in for s, yet its length is not 1. I just don’t see how anything outside of s[0] would be printed to the screen. There’s nothing in that function that looks like it’d print s[1:], let alone in reverse. I’m pretty confused.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:29:31+00:00Added an answer on June 2, 2026 at 6:29 am

    This is a case of recursion, you are calling the function itself over and over with shorter and shorter substrings of the original input, until it is a string of length 1 (the last one in the original input) in which case it starts printing it and then “unwinding” and printing the rest of the string in reverse.

    Take a look at this annoted code:

    def function(s):
        if len(s) == 1:
            print 'single:', s[0],  # (A) this is where your first output is generated for a single character
        else:
            print 'calling function again with ',s[1:]
            function(s[1:]) # (B) you call function again, i.e., your recursive call
            print ' unwind->', s[0], # (C) the "unwinding" step, i.e, finish the rest
                                     # of the function when you return from the recursive call
    

    the output you get is:

    calling function again with 234
    calling function again with 34
    calling function again with 4
    single: 4  unwind-> 3  unwind-> 2  unwind-> 1
    

    The first time you call the function you drop through to the else clause and in line (B) you call the function again, but this time with “234”. Now the function starts again but with “234” and again you drop through to the else and now call function again, but now with “34”, function runs again, now with you drop through to the else once more and call function with just “4” .. this time since it’s of length 1, you print it (line A).

    Now you return from this function (the unwinding process) – and resume from the point where you were before you made your recursive call and you print the rest of the string in reverse by printing the first character of the current remaining character (line C).

    Recursion can be hard to grasp the first time you encounter it – that’s perfectly normal. At some point it will click and become clear. You may want to do some reading about the general concept and search for some clear annotated examples (most CS/programming books will have some).

    Here is a short YouTube video that explains recursion in Python with a simple example, hope it helps: http://www.youtube.com/watch?v=72hal4Cp_2I

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

level: beginner the following code will print 'False' def function(x): if len(x) == 5:
I'm using this simple function: def print_players(players): tot = 1 for p in players:
is there any way I can reproduce this ruby function: def Password.hash(password,salt) Digest::SHA512.hexdigest(#{password}:#{salt}) end
I am writing a script and in my script I have this function: def
I'm trying to solve this newbie puzzle: I've created this function: def bucket_loop(htable, key):
I'm able to go through the first function....but the second func is not running....getaction
I wrote this function: def append_to_all(L, v): '''Append value v, which may be of
Given the following function: def foo(a, b, c): pass How would one obtain a
I have a function: def create(sender, **kw): [...] Which should be called when the
Below is my RUN_SQL function: def RUN_SQL_SAFE(sql, input_tuple=(), get_update_id=False, debug = False): conn =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.