-1

So basically, i am trying to get the user to input something like "123" and recieve the output "3 2 1" but i cant figure out how to add the spaces

# current code
number = str(input("Type out a number with more then 1 charachter: "))
print("Original number:", number)
print("Number in reverse:", number[::-1])

I apologize in advance, im really new to programming.

  • Does this answer your question? [Efficient way to add spaces between characters in a string](https://stackoverflow.com/questions/18221436/efficient-way-to-add-spaces-between-characters-in-a-string) – Gino Mempin Sep 26 '21 at 09:54

3 Answers3

2

Use str.join:

print("Number in reverse:", " ".join(number[::-1]))
user2390182
  • 67,685
  • 6
  • 55
  • 77
2

Use str.join:

print("Number in reverse:", ' '.join(number[::-1]))

Or use an iterator reversed:

print("Number in reverse:", ' '.join(reversed(number)))
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
1

You can use str.join:

print("Number in reverse:", ' '.join(number[::-1]))

Here a space or ' ' is added between the characters.

The join() method returns a string created by joining the elements of an iterable by string separator. exmaples for iterable objects are strings and lists.