0

Unlike C++ cout <<, Python print() function prints the passed arguments with an endline/newline and sometimes I found it annoying having multiple lines in terminal. I know I can use the print(var1, end='') or sys.stderr.write() from sys library but it's too long and quite hard to read. What I think is that may be I can make a cout<< operator in python and prints variables or values without new line at the end

# Example in Python:
for x in range(3):
    print(x)

# Output:
0
1
2

# In C++:    
for (int x = 0; x < 3; x++){
    cout << x << ' ';
}

# output:
0 1 2

Also, '<<' is much easier to type because the key is in the lower portion of the keyboard unlike parenthesis.

Eric Echemane
  • 94
  • 1
  • 9

3 Answers3

2

Use end parameter in print function:

for x in range(3):
    print(x, end=' ')

# output:
0 1 2 
1

you are looking for

print(var1,end=" ")
user13966865
  • 448
  • 4
  • 13
0

Try this:

print(x, end = " ") 
Elletlar
  • 3,136
  • 7
  • 32
  • 38
GDK
  • 74
  • 4