
print("Hello") print("World") Output:
print("Loading", end="...") print("Done") Output: Loading...Done 1. Progress indicator (same line) import time for i in range(5): print(f"\rStep i+1", end="") time.sleep(1)
Python 2 also works with: from __future__ import print_function (then use Python 3 style) ❌ Forgetting the empty string
print("Hello", end) # TypeError ✅
\r returns cursor to line start, overwriting previous text. items = ["apple", "banana", "cherry"] for item in items: print(item, end=" | ") Output: apple | banana | cherry | 3. Building a line gradually sentence = "" for word in ["Python", "is", "fun"]: print(word, end=" ") sentence += word + " " print("\nFinal:", sentence) Differences by Python Version | Version | Without newline | |---------|----------------| | Python 3 | print("text", end="") | | Python 2 | print "text", (trailing comma) |
Hello World Use the end parameter in print() to control what is printed at the end instead of \n . Basic Usage print("Hello", end="") print("World") Output: HelloWorld
print("Hello", end="") ❌ Summary Table | Goal | Code | |------|------| | No newline, nothing after | print("x", end="") | | Space instead of newline | print("x", end=" ") | | Custom separator | print("x", end="---") | | Overwrite same line | print(f"\rx", end="") | Quick Reference Card # Default print("A"); print("B") # A\nB\n No newline print("A", end=""); print("B") # AB Space separator print("A", end=" "); print("B") # A B Custom print("A", end="<-"); print("B") # A<-B
| Yes, life
can be mysterious and confusing--but there's much of life that's
actually rather dependable and reliable. Some principles apply
to life in so many different contexts that they can truly be called
universal--and learning what they are and how to approach them and use
them can teach us some of the most important lessons that we've ever
learned. My doctorate is in Teaching and Learning. I use it a lot when I teach at school, but I also do my best to apply what I've learned to the life I'm living, and to observe how others live their lives. What makes them happy or unhappy, stressed or peaceful, selfish or generous, compassionate or arrogant? In this book, I've done my best to pass on to you what I've learned from people in my life, writers whose works I've read, and stories that I've heard. Perhaps these principles can be a positive part of your life, too! Universal Principles of Living Life Fully. Awareness of these principles can explain a lot and take much of the frustration out of the lives we lead. |
print("Hello") print("World") Output:
print("Loading", end="...") print("Done") Output: Loading...Done 1. Progress indicator (same line) import time for i in range(5): print(f"\rStep i+1", end="") time.sleep(1) python print no newline
Python 2 also works with: from __future__ import print_function (then use Python 3 style) ❌ Forgetting the empty string Building a line gradually sentence = "" for
print("Hello", end) # TypeError ✅
\r returns cursor to line start, overwriting previous text. items = ["apple", "banana", "cherry"] for item in items: print(item, end=" | ") Output: apple | banana | cherry | 3. Building a line gradually sentence = "" for word in ["Python", "is", "fun"]: print(word, end=" ") sentence += word + " " print("\nFinal:", sentence) Differences by Python Version | Version | Without newline | |---------|----------------| | Python 3 | print("text", end="") | | Python 2 | print "text", (trailing comma) | end="") print("World") Output: HelloWorld print("Hello"
Hello World Use the end parameter in print() to control what is printed at the end instead of \n . Basic Usage print("Hello", end="") print("World") Output: HelloWorld
print("Hello", end="") ❌ Summary Table | Goal | Code | |------|------| | No newline, nothing after | print("x", end="") | | Space instead of newline | print("x", end=" ") | | Custom separator | print("x", end="---") | | Overwrite same line | print(f"\rx", end="") | Quick Reference Card # Default print("A"); print("B") # A\nB\n No newline print("A", end=""); print("B") # AB Space separator print("A", end=" "); print("B") # A B Custom print("A", end="<-"); print("B") # A<-B