I am new to Python and still cannot do this little program on my own. (I am currently doing it in excel and I want to write a code in python so that it does not take me tons of time every time I do it) I receive numbers in the format: 1234567891/ 9654321878/ 9283876326/ ...etc (all numbers are one below the other without /) then I want to Python to print: 123 456 789 1/ 965 432 187 8/ 928 387 632 6/ ...etc (all numbers one below the other without .) Can anyone help me with this code? Thank you!
-
number = int(input()) number = str(number) while number != "break": print(number[0:3], end =" ") print(number[3:6], end =" ") print(number[6:9], end =" ") print(number[9]) number = int(input()) number = str(number) – crazzssy Nov 10 '21 at 16:21
2 Answers
The way to go about this is to loop through each number, you convert it to a string using the str() function.
Then you can use string slicing as shown in this question to print out each group of digits.
If the task is to format 10 digits specifically, you could hard code the slicing with the specific indecies at which you want the slicing to happen (ex. 2, 5, 7 and 9 as shown in your example) or write it so that a new group is printed every 3 digits until the string is fully passed through.
At the end of the iteration print a newline character: \n
.

- 468
- 4
- 23
let me see if i understood your question? You want to convert a "string" of numbers into a a new string where the numbers are divided into groups of 3 numbers? You can do that with a function:
def convertStringOfInts (numbers):
"""
numbers > a string of numbers
output > the originaly string divided into groups of 3 numbers
"""
numbers.replace(" ", "")
newNumbers = ""
a = 1
for b in numbers:
if a%3!=0:
newNumbers += b
else:
newNumbers += b+" "
a+=1
return newNumbers
I hope this can help you, or that you can adapt it if it is not the anwser that you are looking for. Cheers!

- 1
-
It looks like there are chunks of numbers possibly not a multiple of 3 (seems to be 10 digits each although he doesn't specify clearly whether it can be a different number), so the last digit is by itself. Additionally, he wants to separate these numbers with `"/` ". – user904963 Nov 09 '21 at 10:22
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 09 '21 at 10:31
-
Hello, Let me clarify, I receive many 10 digit numbers in an excel table one below the other in a column. (there is no spacing in each number for ex. 7659876547. Then below it another 10 digit number for example 8760986547, etc and each time I receive different amount of numbers one below the other. I want to copy those numbers from the excel and paste them in python and then for python to give me the numbers in the format - 3 digits, space, 3 digits, space, 3 digits space, 1 digit - for each number and to give them one below the other again. Hence 1234567890 to become 123 456 789 0 etc. – crazzssy Nov 10 '21 at 14:50