-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
  • 72,016
  • 6
  • 67
  • 89
2

Use str.join:

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

Or use an iterator reversed:

print("Number in reverse:", ' '.join(reversed(number)))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • [`str.join` is typically less performant with a generator than with a sequence](https://stackoverflow.com/questions/37782066/list-vs-generator-comprehension-speed-with-join-function). – user2390182 Sep 26 '21 at 09:58
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.