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.
Related
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:
How can I print multiple things on the same line, one at a time?
(18 answers)
Print in one line dynamically [duplicate]
(22 answers)
Closed 3 years ago.
I have a program that has to only print data onto one line.
What can I do to do this.
for i in range(10):
print(i)
Is it possible to print all of this on one line so it prints 0, erases the line, print 2, erases, etc..?
Use print(i,end="\r") to return to the start of the line and overwrite.
for i in range(10):
print(i,end=" ")
this is easiest way to print in one line.
in python 2.x:
from __future__ import print_function
for i in range(10):
print (i, end="")
in python 3.x
for i in range(10):
print (i, end="")
For this specific usecase you can do something like this:
print(*range(10))
And to update each character on the line you will need to use '\r' or the return character, that returns the position of the cursor to the beginning of the line. However, you need to be sure you count in the length of the strings you are printing, otherwise you will be overwriting only part of the string. A full proof solution will be:
import time
maxlen = 0
for i in range(12,-1,-1):
if len(str(i))>maxlen:
maxlen = len(str(i))
print(f'\r{str(i): <{maxlen}}',end = '')
time.sleep(2)
print('')
time part is added so that you can view the change. maxlen computes the maximum length string you are going to print and formats the string accordingly. Note: I have used f'strings, hence it would only work for Python 3.x
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
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 7 years ago.
Let's say
t1=loadtxt("t\\1.txt")
t2=loadtxt("t\\2.txt")
Assume inside 1.txt is
1 2 3
4 5 6
Assume also inside 2.txt is
1 2 3
4 5 6
#
t1[0,0]=1
t1[0,1]=2
t1[0,2]=3
t2[0,0]=1
t2[0,1]=2
t2[0,2]=3
st=0
for i in range(2):
for j in range(3):
a='t'+str(i+1)
st=st+a
I got "TypeError: string indices must be integers"
What I want this piece of code to do is
st=t1[0,0]+t1[0,1]+t1[0,2]+t2[0,0]+t2[0,1]+t2[0,2]
Then how to fulfill this goal?
How to represent the array name with regular numbers in for loop? Instead of summing each value one by one.
Well you could either use a dictionary with all your t's or use the exec built in function. Python 2.x looks like this:
t1 = [1,2,3]
t2 = [1,2,3]
st=0
for i in range(2):
for j in range(3):
exec "a = t"+str(i+1)+"["+str(j)+"]"
st+=a
print st
In Python 3.x you want to use () for the built in functions. Makes the code look like this:
t1 = [1,2,3]
t2 = [1,2,3]
st=0
for i in range(2):
for j in range(3):
exec("a = t"+str(i+1)+"["+str(j)+"]")
st+=a
print(st)
The exec function will take a string and act like it was a normal line of code and execute it. This way you can assemble your command and let the interpreter do the rest. (Using String Formatting instead of connecting the string with + might be a better approach though)
There are ways to do what you want (see link in the comment to your question), but why bother when you can simply do:
st = 0
for t in (t1,t2):
for i in range(3):
st += t[0,i]
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