Python's print()
command always moves to a new line. If you don't want that, you have a couple of choices. Type in this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Alpha Bravo Charlie Delta EchoFoxtrotGolf EchoFoxtrotGolf
The print()
function behaves slightly differently in Python 2 and Python 3. We want the Python 3 behavior, so the first line tells Python to import the behavior from the future. (That's two underscores around the word future
.)
The second line imports the sys
library so we can use sys.stdout.write()
. You won't need this most of the time.
Did you notice on line 7 that we used apostrophes (single quotes) instead of regular quotes? In Python, they are pretty much interchangable.
On line 11, we want to display "Echo"
, but we don't want to move to a new line afterward. The way to do this in Python 3 is with the end
attribute as shown.
Another way to accomplish the same thing is using sys.stdout.write()
. (This way also works in Python 2.)
What happens if the open quote and the close quote don't match? Try it and answer in a comment.
What happens if you put something between the
end=""
quotes on line 11? Try it and answer in a comment.
©2017 Graham Mitchell