Print Characters Recursively

Print each character of string recursively.

Logic BuildingAdvanced
Logic Building
def print_chars(s, index=0):
    # Base case
    if index >= len(s):
        return
    
    # Print current character
    print(s[index])
    # Recurse
    print_chars(s, index + 1)

# Test
text = input("Enter a string: ")
print_chars(text)

Output

Enter a string: Hello
H
e
l
l
o

Print current character, recurse for next.

Key Concepts:

  • Base case: index >= len(s)
  • Print character at index
  • Recurse with index + 1