Browse Source

Adding mention of `end` parameter for `print`

Based on:

From: sourin.sutradhar@gmail.com

Hi Swaroop,

I am a C/C++ programmer. Recently I have started learning python from
your book. Your way of writing is really awesome and clean. As for
suggestion, I think it would be nice if you add a small section on
formatted output.

For example:

In your PDF version of Byte of python, at the end of page 44 in
section8.3, you have described a program to print numbers using for
loop. But as per the output all the numbers are getting printed in new
lines. This is confusing , especially for C/C++/Java programmers. As for
such situations you can add a note describing that to avoid the newline,
one can also write is as follows (though there are many other methods):

for i in range(1,5):
   print(i, end=' ')
#to avoid newline

Or you can add a whole section in the book to descrive formatted output
in python. That will be helpful for beginners.

Thank you Swaroop. Really enjoying your book on Python.

Best of luck.
Swaroop C H 12 years ago
parent
commit
25c40b765c
1 changed files with 17 additions and 0 deletions
  1. 17 0
      06-basics.md

+ 17 - 0
06-basics.md

@@ -177,6 +177,23 @@ detailed specifications such as:
 >>> '{name} wrote {book}'.format(name='Swaroop', book='A Byte of Python')
 ~~~
 
+Since we are discussing formatting, note that `print()` always ends
+with an invisible "new line" character (`\n`) so that repeated calls
+to `print()` will all print on a separate line each. To prevent this
+newline character from being printed, you can override the `end`
+parameter to `print`:
+
+~~~python
+print("a", end="")
+print("b", end="")
+~~~
+
+Output:
+
+~~~
+ab
+~~~
+
 ### Escape Sequences ###
 
 Suppose, you want to have a string which contains a single quote