[Tutor] Printing with no newline :(

Peter Otten __peter__ at web.de
Sun Nov 6 08:41:58 CET 2011

Joe Batt wrote:

> I am learning Python 3 and programming and am very new so please bear with
> me…
> I am writing a program to pull out specific characters in a sequence and
> then print then out. So far so good however when the characters are
> printed out they pint on separate lines as opposed to what I want, all on
> the same line. I have tried \n and just ,  in the pint statement i.e.
> print(letterGroup[4],) and print(letterGroup[4]\n) and even
> print(letterGroup[4],/n)…….. Can anyone help and explain please….Thank you

The following arrived in a totally messed up formatting:

> for line in file:    
>     m = re.search(regexp, line)    
>     if m:
>         letterGroup = m.group(0)        
>         print(letterGroup[4])

You can specify what to print after the argument(s) with the end keyword 

>>> items = 1, 2, 3
>>> for item in items:
...     print(item, end=" ")
1 2 3 >>>
>>> for item in items:
...     print(item, end="")
>>> for item in items:
...     print(item, end="WHATEVER")

The default for end is of course newline, spelt "\n" in a Python string 
literal. Use

>>> help(print)

in the interactive interpreter to learn more about the print() function.

More information about the Tutor mailing list