Question: I already know how to count down and up using reccurssive function but I cant figure out how to define another function to have spaces in a pattern like this:
[Desired output](https://i.stack.imgur.com/KhIw2.png)

My code for counting down and up:

```
def f(n):
if n==0:
    print (n)  
    return
print (n) 
f(n-1)  
print (n)  

```

Answer: 
I am posting this to contrast the other answers provided here -

```py
def f(n, s = ""):
  if n <= 0:
    print(s, n)
  else:
    print(s, n)
    f(n - 1, s + " ")
    print(s, n)

f(5)

```

```none
 5
  4
   3
    2
     1
      0
     1
    2
   3
  4
 5

```

The program could be further simplified, if desired -

```py
def f(n, s = ""):
  print(s, n)
  if n > 0:
    f(n - 1, s + " ")
    print(s, n)

f(5)

```

Default parameter `s` could be an integer instead of a string, if desired -

```py
def f(n, s = 0):
  print(" "*s, n)
  if n > 0:
    f(n - 1, s + 1)
    print(" "*s, n)

f(5)

```

Output is the same.

---

`print` adds a space between the printed arguments. To remove this, we can print a single formatted value -

```py
def f(n, s = ""):
  print(s + str(n))          # <- single value
  if n > 0:
    f(n - 1, s + " ")
    print(s + str(n))        # <- single value

```

Or you can use a [formatted string literal](https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings) -

```py
def f(n, s = ""):
  print(f"{s}{n}")     # <- formatted string
  if n > 0:
    f(n - 1, s + " ")
    print(f"{s}{n}")   # <- formatted string

```
This is probably not exactly what you need. But it gives you an idea :)

As you can see it is based on @Samudra Ganguly's answer

```
def f(n,x):
    if n < 0:
        return
    p(n,x-n)
    f(n-1,x)
    if n !=0:
        p(n,x-n)

def p(m,x):
    print(" ", end = "")
    if x>0:
        p(m,x-1)
    else:
        print(m, end = "\n")

f(8,8)

```

[![enter image description here](https://i.stack.imgur.com/p8LMZ.png)](https://i.stack.imgur.com/p8LMZ.png)

Also you can change the function like this:

```
k=4;

def f(n):
    if n < 0:
        return
    p(n,k-n)
    f(n-1)
    if n !=0:
        p(n,k-n)

def p(m,x):
    print(" ", end = "")
    if x>0:
        p(m,x-1)
    else:
        print(m, end = "\n")

f(k)

```