-2

Write a program that prompts the user to enter one number consisting of THREE digits, separate the number into its individual digits and prints the digits separated from one another by a space and a comma. I made three individual digits into a list. And the except output should be no square brackets. Please help. Many Thanks!

input_integer = int(input("Enter three-digit integer: "))
s = [int(i) for i in str(input_integer)]

print("Digits in",input_integer, "are",s)

Here is what I except:

Enter three-digit integer: 123 Digits in 123 are 1, 2, 3

Mark
  • 90,562
  • 7
  • 108
  • 148
  • `input` gives a string. There's no reason to turn it to an `int` only to turn it back into a `string`. But you've close with what you have — you are missing a `join()` of some sort to put the list back together. – Mark Aug 01 '19 at 20:22
  • I think you want this. https://stackoverflow.com/questions/1906717/splitting-integer-in-python – Ashiq Ullah Aug 01 '19 at 20:27
  • Possible duplicate of [Print list without brackets in a single row](https://stackoverflow.com/questions/11178061/print-list-without-brackets-in-a-single-row) – norok2 Aug 01 '19 at 20:28

1 Answers1

0

You are almost there. The brackets get printed because you have you are printing the entire list. Also you are swinging between int ant str somewhat unnecessarily. One way of printing only the digits is to join each element with a separator of your choice, e.g. ,.

So one possible way to achieve what you want is (using f-string interpolation):

number = input("Enter three-digit integer: ")
digits = list(number)

print(f"Digits in {number} are {', '.join(digits)}")
Enter three-digit integer: 123
Digits in 123 are 1, 2, 3
norok2
  • 25,683
  • 4
  • 73
  • 99