This question already has answers here:
Print in one line dynamically [duplicate]
(22 answers)
Closed 9 years ago.
I know how to type numbers from python.
>>> for a in range(1,11):
print(a)
1
2
3
4
5
6
7
8
9
10
Here the output is given in one line after the other.
So I want to type the numbers in the same line without using lists and stacks. I that possible?
Then how can I do that?
print automatically adds a newline character after the string you've entered, this is why it prints each number on a different line. To change this behavior, you must change end parameter on the function call.
for a in range(1,11):
print(a, end=' ')
Edit:
The end parameter holds a string which gets printed after the string you've entered. By default it's set to \n so after each print, \n is added:
print("Hello!") # actually prints "Hello!\n"
You can change the parameter to anything you like:
print("Hello!", end="...") # prints "Hello!..."
print("Hello!", end="") # prints "Hello!"
Try this:
>>> print(' '.join(str(i) for i in range(1,11)))
1 2 3 4 5 6 7 8 9 10
Related
This question already has answers here:
multiplication of two arguments in python [duplicate]
(6 answers)
Getting issue while trying to multiply string and integer [duplicate]
(2 answers)
Closed 3 months ago.
I want to output 5 * 2 = 10 but python output is 55!
How do I resolve this problem?
a = 0
b = 2
a = input("a? :") #(get 5 as input)
c = a * b
print (c)
This is my code.
when I input a number it repeat same number I entered two times insterd of showing multipiy it. What do I have to do to solve this?
a is a string,
so it will be
'5'*2='55'
if you want 10, you need to cast a to int.
a=int(input())
here is the link to document
https://docs.python.org/3/library/functions.html#input
This question already has answers here:
Why does returning in Interactive Python print to sys.stdout?
(5 answers)
Closed 3 years ago.
I wrote an extremely basic python code which creates a file and appends 10 lines in it
>>> b = open("b.txt", 'w')
>>> for i in range(10):
... b.write("line %d\n" %i)
...
7
7
7
7
7
7
7
7
7
7
Why are 7's outputted on the screen?
This comes from the behavior of the interactive interpreter. You'll notice a similar effect if you write
>>> x = 5
>>> x
5
When you have a statement and don't set it equal to anything, the interactive interpreter will repr the output/return value and display it in the terminal. The return value for a file write command is the number of bytes written to the file.
The '7's outputted to the console are the number of bytes written to the file, and are returned by the file.write function. See the docs here.
In addition, you should generally use the following format to handle files:
with open("b.txt", "w") as f:
for i in range(10):
f.write("line %d\n" %i)
This avoids having to manually close the file after you've finished with it.
This question already has answers here:
How can I print multiple things on the same line, one at a time?
(18 answers)
Closed 3 years ago.
I want to know how to print output in a single line
I want to print it like: 1234
instead of
1
2
3
4
Code:
# n = (get in from user)
i=1
while (i<=n):
print (i,)
i +=1
This may help :
For Python 3:
print(i,end='')
For Python 2:
print(i),
This question already has answers here:
Why are str.count('') and len(str) giving different output?
(3 answers)
Closed 4 years ago.
I was going through something when i came across this...
a = 'hello'
print(a.count('l'))
print(a.count(''))
Output is :
2
6
Can anyone explain why the second output is 6 ?
count here returns the number of occurences of your substring, here a '' string.
There are indeed six '' in hello, between each char:
print(''.count('')) # >> 1
# You could symbolize it like that:
print('a'.count('')) # >> 2
# is equivalent to:
print('|a|'.count('|')) # >> 2
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 5 years ago.
for example, I want to print
01234567
so I know I need to write
for i in range(8)
print(i),
and it will print 0 1 2 3 4 5 6 7
instead of change the line for every number
but I want it whithout spaces
You can use some Python 3 features in Python 2:
from __future__ import print_function
for i in range(8):
print(i, end='')
Output:
01234567