python – Printing numbers in a row without using string
python – Printing numbers in a row without using string
You can use end
keyword in print()
method to avoid spaces:
num = int(input(Enter number: ))
for i in range(1, num+1):
print(i, end=)
You may create a custom function using range()
along with the usage of math.log10()
as:
import math
def get_my_number(num):
my_num = 0
for i in range(1, num+1):
digits = int(math.log10(i))+1 # count number of digits in number
my_num = my_num * (10 ** digits) + i
return my_num
Sample run:
>>> get_my_number(3)
123
>>> get_my_number(13) # For number greater than 10
12345678910111213
This way you will get the actual value and you wont be just printing it on the console
python – Printing numbers in a row without using string
for statement below takes elements starting from 1 to n.
 If you use in range(n) it would print the elements starting from 0 to n1.
 If you use in range(1,n) it would print the elements starting from 1 to n1.
 If you use in range(1,n+1) it would print the elements starting from 0 to n. And this is what you want.
printing without new line requires and addition in print command.

end = would print 123 for example.

end = with 1 space between would print 1 2 3 and so on.
if __name__ == __main__: n = int(input()) for i in range(1,n+1): print (i, end = )