|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/courses/python-print/continue/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/courses/python-print/continue/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Fvideos%2Fbasic-usage-and-string-literals%2F |
| https://realpython.com/courses/python-print/#team |
| The Python print() Function: Go Beyond the Basics | https://realpython.com/courses/python-print/ |
| Christopher Trudeau | https://realpython.com/courses/python-print/#team |
| Recommended Tutorial | https://realpython.com/python-print/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-print/downloads/slides-print/ |
| Sample Code (.zip) | https://realpython.com/courses/python-print/downloads/code-print/ |
| Ask a Question | https://realpython.com/courses/python-print/continue/#discussion |
| https://realpython.com/feedback/survey/course/python-print/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-print/disliked/?from=lesson-title |
| Contents | https://realpython.com/courses/python-print/continue/#description |
| Transcript | https://realpython.com/courses/python-print/continue/#transcript |
| Discussion (10) | https://realpython.com/courses/python-print/continue/#discussion |
| Sample Code (.zip) | https://realpython.com/courses/python-print/downloads/code-print/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-print/downloads/slides-print/ |
| 00:00 | https://realpython.com/courses/python-print/continue/#t=0.48 |
| Welcome! This course is on the print() function in Python. | https://realpython.com/courses/python-print/continue/#t=0.48 |
| My name is Chris, and I will be your guide. | https://realpython.com/courses/python-print/continue/#t=3.39 |
| I will be covering how string literals work in Python; | https://realpython.com/courses/python-print/continue/#t=6.36 |
| how to format those strings and use them inside of print(); | https://realpython.com/courses/python-print/continue/#t=9.69 |
| the sep, end, and flush arguments that have been introduced in the new print() | https://realpython.com/courses/python-print/continue/#t=13.26 |
| function in Python 3; | https://realpython.com/courses/python-print/continue/#t=17.46 |
| how to print to files and streams besides just to stdout (standard out); how to adjust your | https://realpython.com/courses/python-print/continue/#t=19.62 |
| own data types so they look better when they’re printed; | https://realpython.com/courses/python-print/continue/#t=24.27 |
| what changed in print() between Python 2 in Python 3 and the advantages you | https://realpython.com/courses/python-print/continue/#t=27.66 |
| can use now that print() is a built-in function; | https://realpython.com/courses/python-print/continue/#t=32.159 |
| the alternative to print(), the pretty print command; | https://realpython.com/courses/python-print/continue/#t=35.1 |
| how to use ANSI escape sequences to colorize and add bold and underline to your | https://realpython.com/courses/python-print/continue/#t=37.8 |
| text; using control characters to produce simple line animation; | https://realpython.com/courses/python-print/continue/#t=42.51 |
| how to mock print() inside of unit tests; how to use print() inside of debugging; | https://realpython.com/courses/python-print/continue/#t=47.28 |
| and then finally, | https://realpython.com/courses/python-print/continue/#t=52.11 |
| I’ll talk a little bit about other places you can go to improve your output | https://realpython.com/courses/python-print/continue/#t=52.89 |
| game. A quick note, | https://realpython.com/courses/python-print/continue/#t=56.61 |
| before I start: print() is one of the more obvious changes between Python 2 and | https://realpython.com/courses/python-print/continue/#t=58.59 |
| Python 3. print used to be a built-in keyword statement and is now a built-in | https://realpython.com/courses/python-print/continue/#t=62.4 |
| function. Lesson 6 talks about specific differences, but elsewhere, | https://realpython.com/courses/python-print/continue/#t=67.41 |
| all of the examples are in Python 3, | https://realpython.com/courses/python-print/continue/#t=72.09 |
| so the code you see won’t work in a Python 2 environment. | https://realpython.com/courses/python-print/continue/#t=74.58 |
| 01:18 | https://realpython.com/courses/python-print/continue/#t=78.36 |
| Let’s start with the simplest example—just typing print() into the REPL. | https://realpython.com/courses/python-print/continue/#t=78.36 |
| 01:24 | https://realpython.com/courses/python-print/continue/#t=84.39 |
| It returns an empty line. That’s because print() expects a string and then appends a | https://realpython.com/courses/python-print/continue/#t=84.39 |
| newline after it. Something a little more useful—printing a greeting… | https://realpython.com/courses/python-print/continue/#t=89.52 |
| 01:36 | https://realpython.com/courses/python-print/continue/#t=96.51 |
| and there you go. The output is whatever string was passed to print(). | https://realpython.com/courses/python-print/continue/#t=96.51 |
| You can also pass multiple arguments to print()—not just strings, | https://realpython.com/courses/python-print/continue/#t=101.1 |
| but numbers or other kinds of objects. print() | https://realpython.com/courses/python-print/continue/#t=105.12 |
| converts them to strings and then prints out the output. Strings are how you | https://realpython.com/courses/python-print/continue/#t=108.84 |
| interface with text in your code. | https://realpython.com/courses/python-print/continue/#t=113.76 |
| 01:55 | https://realpython.com/courses/python-print/continue/#t=115.8 |
| There are three ways of declaring a string inside of Python. | https://realpython.com/courses/python-print/continue/#t=115.8 |
| The first is to use double quotes ("), the second is single quotes ('), | https://realpython.com/courses/python-print/continue/#t=119.31 |
| and the third is triple quotes ("""). | https://realpython.com/courses/python-print/continue/#t=123.33 |
| 02:05 | https://realpython.com/courses/python-print/continue/#t=125.76 |
| Triple quotes allow you to represent multiline strings. Personally, | https://realpython.com/courses/python-print/continue/#t=125.76 |
| I use single quotes, but the Python style guide says to be consistent. | https://realpython.com/courses/python-print/continue/#t=130.05 |
| Triple quotes are also used to create docstrings inside of your code. | https://realpython.com/courses/python-print/continue/#t=134.85 |
| 02:19 | https://realpython.com/courses/python-print/continue/#t=139.8 |
| Supporting the two kinds of quotes makes it easy to put quotes inside of | https://realpython.com/courses/python-print/continue/#t=139.8 |
| strings. For example, this first sentence has an apostrophe in it. | https://realpython.com/courses/python-print/continue/#t=143.16 |
| I don’t have to do anything special, because the double quotes on the outside | https://realpython.com/courses/python-print/continue/#t=147.54 |
| allow me to put the single quote on the inside without making any differences. | https://realpython.com/courses/python-print/continue/#t=151.44 |
| 02:36 | https://realpython.com/courses/python-print/continue/#t=156.15 |
| Likewise, by putting a string with single quotes on the outside, | https://realpython.com/courses/python-print/continue/#t=156.15 |
| I can put the double quotes on the inside without having to do anything special. | https://realpython.com/courses/python-print/continue/#t=159.42 |
| 02:44 | https://realpython.com/courses/python-print/continue/#t=164.4 |
| This can be particularly helpful if you’re showing HTML where there’s a lot of | https://realpython.com/courses/python-print/continue/#t=164.4 |
| quotes inside of the HTML itself. | https://realpython.com/courses/python-print/continue/#t=168.3 |
| 02:52 | https://realpython.com/courses/python-print/continue/#t=172.89 |
| Let’s look at that inside of the REPL. | https://realpython.com/courses/python-print/continue/#t=172.89 |
| I’ll start with a double-quote string. Enter it in. Python, | https://realpython.com/courses/python-print/continue/#t=174.84 |
| interestingly enough, shows that in the REPL as a single quote, | https://realpython.com/courses/python-print/continue/#t=179.65 |
| but it’s showing you the string I’ve just entered. | https://realpython.com/courses/python-print/continue/#t=182.98 |
| 03:05 | https://realpython.com/courses/python-print/continue/#t=185.26 |
| Same thing happens if I do it with single quotes. With a multiline quote, | https://realpython.com/courses/python-print/continue/#t=185.26 |
| the REPL stops when I hit Enter, | https://realpython.com/courses/python-print/continue/#t=190.12 |
| allows me to keep typing on my multiple bits of input until I close it with the | https://realpython.com/courses/python-print/continue/#t=192.79 |
| triple quotes. | https://realpython.com/courses/python-print/continue/#t=198.01 |
| 03:19 | https://realpython.com/courses/python-print/continue/#t=199.36 |
| It returns that as a single quote with \n’s inside of it | https://realpython.com/courses/python-print/continue/#t=199.36 |
| where my new lines were. Let’s take a closer look at that. | https://realpython.com/courses/python-print/continue/#t=204.28 |
| Here’s the code I just entered inside of the REPL. | https://realpython.com/courses/python-print/continue/#t=209.38 |
| 03:32 | https://realpython.com/courses/python-print/continue/#t=212.56 |
| You’ll notice that it returns with these \n’s. | https://realpython.com/courses/python-print/continue/#t=212.56 |
| What’s a \n? This represents what’s called a newline. | https://realpython.com/courses/python-print/continue/#t=215.71 |
| Different operating systems handle this character differently, | https://realpython.com/courses/python-print/continue/#t=220.48 |
| but Python abstracts that away for you. | https://realpython.com/courses/python-print/continue/#t=223.54 |
| 03:46 | https://realpython.com/courses/python-print/continue/#t=226.09 |
| The newline character creates a new line in your output, | https://realpython.com/courses/python-print/continue/#t=226.09 |
| returning the cursor to the beginning. By default, print() automatically adds one of | https://realpython.com/courses/python-print/continue/#t=228.58 |
| these to the end of any string that it prints. | https://realpython.com/courses/python-print/continue/#t=233.89 |
| 03:57 | https://realpython.com/courses/python-print/continue/#t=237.55 |
| So if I was going to print that string | https://realpython.com/courses/python-print/continue/#t=237.55 |
| that is in the code above, a third newline would show up after the word 'lines', | https://realpython.com/courses/python-print/continue/#t=239.44 |
| 04:05 | https://realpython.com/courses/python-print/continue/#t=245.68 |
| returning the cursor to the beginning. \n | https://realpython.com/courses/python-print/continue/#t=245.68 |
| isn’t the only special character. These slash ( \ ) characters are called | https://realpython.com/courses/python-print/continue/#t=249.73 |
| escape characters. Newline (\n) does a return to the beginning. Tab (\t) shows a tab. | https://realpython.com/courses/python-print/continue/#t=253.68 |
| 04:19 | https://realpython.com/courses/python-print/continue/#t=259.48 |
| ASCII bell (\a) actually plays a little “boonk” sound. | https://realpython.com/courses/python-print/continue/#t=259.48 |
| You can put in octal or hex character values by using the \o or \x | https://realpython.com/courses/python-print/continue/#t=263.26 |
| escape. | https://realpython.com/courses/python-print/continue/#t=269.11 |
| 04:30 | https://realpython.com/courses/python-print/continue/#t=270.55 |
| If you actually have a slash ( \ ) that you need to put in, writing a slash ( \ ) is slash slash ( \\ ). | https://realpython.com/courses/python-print/continue/#t=270.55 |
| You can also escape quotes. Slash double quote (\") and slash single quote (\') | https://realpython.com/courses/python-print/continue/#t=277.03 |
| allow you to put a quote inside of a string | https://realpython.com/courses/python-print/continue/#t=281.5 |
| if you had to put a single quote inside of a single-quoted string or a double | https://realpython.com/courses/python-print/continue/#t=284.05 |
| quote inside of a double-quoted string. Lastly, | https://realpython.com/courses/python-print/continue/#t=287.65 |
| you can also move the cursor back one and cause a carriage return, | https://realpython.com/courses/python-print/continue/#t=291.04 |
| which moves it to the beginning of the line. | https://realpython.com/courses/python-print/continue/#t=295.63 |
| 04:57 | https://realpython.com/courses/python-print/continue/#t=297.91 |
| These last two characters are useful for doing animation, | https://realpython.com/courses/python-print/continue/#t=297.91 |
| which I’ll talk about in a later lesson. | https://realpython.com/courses/python-print/continue/#t=300.94 |
| 05:05 | https://realpython.com/courses/python-print/continue/#t=305.08 |
| Let’s look at some of these in action. | https://realpython.com/courses/python-print/continue/#t=305.08 |
| I’m going to start by entering a string with some newlines and some tabs in it. | https://realpython.com/courses/python-print/continue/#t=307.3 |
| 05:13 | https://realpython.com/courses/python-print/continue/#t=313.45 |
| With each "\n" | https://realpython.com/courses/python-print/continue/#t=313.45 |
| I get a new line of text, and with the "\t" I get a tab moving my text | https://realpython.com/courses/python-print/continue/#t=314.56 |
| in by eight spaces. | https://realpython.com/courses/python-print/continue/#t=319.87 |
| Let’s try some low-grade audio fun. Printing a "\a" makes a “bong” noise. | https://realpython.com/courses/python-print/continue/#t=322.33 |
| 05:29 | https://realpython.com/courses/python-print/continue/#t=329.5 |
| The sound you get will actually vary depending on your operating system, and in | https://realpython.com/courses/python-print/continue/#t=329.5 |
| newer operating systems, | https://realpython.com/courses/python-print/continue/#t=333.1 |
| you can actually set the sound that gets played. In old operating systems, | https://realpython.com/courses/python-print/continue/#t=334.09 |
| this was just a little “boop” noise out of your speaker. | https://realpython.com/courses/python-print/continue/#t=338.53 |
| 05:42 | https://realpython.com/courses/python-print/continue/#t=342.49 |
| Text inside of your computer is represented in an encoding. | https://realpython.com/courses/python-print/continue/#t=342.49 |
| One of the older encodings that is common through most computers is something | https://realpython.com/courses/python-print/continue/#t=346.12 |
| called ASCII. For example, | https://realpython.com/courses/python-print/continue/#t=349.57 |
| capital letter 'A' in 'ASCII' is the number 65. | https://realpython.com/courses/python-print/continue/#t=351.79 |
| 05:55 | https://realpython.com/courses/python-print/continue/#t=355.66 |
| You can represent numbers in decimal, octal, and hexadecimal notations, | https://realpython.com/courses/python-print/continue/#t=355.66 |
| and in print(), you can use that to print out special characters. | https://realpython.com/courses/python-print/continue/#t=360.56 |
| So octal 275, which is decimal 189, is ½, | https://realpython.com/courses/python-print/continue/#t=365.87 |
| or at least it is in the font I’m using. | https://realpython.com/courses/python-print/continue/#t=370.91 |
| 06:12 | https://realpython.com/courses/python-print/continue/#t=372.71 |
| It varies depending on what your encodings are. | https://realpython.com/courses/python-print/continue/#t=372.71 |
| I can now do the same thing using hex. | https://realpython.com/courses/python-print/continue/#t=376.01 |
| 275 hex is BD and I get the same character. | https://realpython.com/courses/python-print/continue/#t=380.04 |
| 06:27 | https://realpython.com/courses/python-print/continue/#t=387.53 |
| How about some embedded quotes? | https://realpython.com/courses/python-print/continue/#t=387.53 |
| I can escape a \" or a \' so that I can put quotes inside | https://realpython.com/courses/python-print/continue/#t=389.93 |
| of my quotes no matter how I’ve created strings. The backslash ( \ ) character is | https://realpython.com/courses/python-print/continue/#t=395.69 |
| how you formulate an escape sequence. So, what if you only want a backslash? | https://realpython.com/courses/python-print/continue/#t=400.31 |
| 06:43 | https://realpython.com/courses/python-print/continue/#t=403.94 |
| Well, backslash backslash ( \\ ) gives you a single backslash ( \ ). | https://realpython.com/courses/python-print/continue/#t=403.94 |
| 06:49 | https://realpython.com/courses/python-print/continue/#t=409.37 |
| Easy enough. Now, something a little trickier. | https://realpython.com/courses/python-print/continue/#t=409.37 |
| How about the carriage return? \r puts the cursor back to the beginning of | https://realpython.com/courses/python-print/continue/#t=412.79 |
| the line. | https://realpython.com/courses/python-print/continue/#t=417.86 |
| This can cause some weirdnesses. Notice that what gets printed here is only part | https://realpython.com/courses/python-print/continue/#t=419.69 |
| of our string. "Carriage return means go back \r", | https://realpython.com/courses/python-print/continue/#t=424.4 |
| then the cursor goes to the beginning of the line and starts overwriting with | https://realpython.com/courses/python-print/continue/#t=429.14 |
| "to the start of the line". | https://realpython.com/courses/python-print/continue/#t=433.7 |
| 07:15 | https://realpython.com/courses/python-print/continue/#t=435.62 |
| Because "to the start of the line" is shorter than the first segment, | https://realpython.com/courses/python-print/continue/#t=435.62 |
| you still get the word "back" trailing at the remainder. | https://realpython.com/courses/python-print/continue/#t=439.58 |
| 07:23 | https://realpython.com/courses/python-print/continue/#t=443.72 |
| This is a little messy, but it’s actually useful when you do animation, | https://realpython.com/courses/python-print/continue/#t=443.72 |
| which I will talk about in a later lesson. | https://realpython.com/courses/python-print/continue/#t=447.53 |
| 07:31 | https://realpython.com/courses/python-print/continue/#t=451.82 |
| There are certain kinds of strings where backslashes are common. Windows | https://realpython.com/courses/python-print/continue/#t=451.82 |
| file paths and regular expressions frequently use them. | https://realpython.com/courses/python-print/continue/#t=455.36 |
| This makes putting backslash backslash ( \\ ) inside of your strings on all the | https://realpython.com/courses/python-print/continue/#t=459.05 |
| backslashes kind of messy. You can get some pretty ugly strings pretty quickly. | https://realpython.com/courses/python-print/continue/#t=462.83 |
| 07:48 | https://realpython.com/courses/python-print/continue/#t=468.02 |
| Python provides a way to get around this, and that’s the raw string. | https://realpython.com/courses/python-print/continue/#t=468.02 |
| You prefix an r in front of the string and everything inside of it will ignore | https://realpython.com/courses/python-print/continue/#t=471.71 |
| escape characters. | https://realpython.com/courses/python-print/continue/#t=476.15 |
| 07:57 | https://realpython.com/courses/python-print/continue/#t=477.86 |
| This allows you to have a lot of cleaner-to-read strings in the case where you | https://realpython.com/courses/python-print/continue/#t=477.86 |
| have a lot of backslashes. | https://realpython.com/courses/python-print/continue/#t=481.1 |
| 08:03 | https://realpython.com/courses/python-print/continue/#t=483.92 |
| One final trick that I like to use is you can multiply strings to tell Python to | https://realpython.com/courses/python-print/continue/#t=483.92 |
| repeat them. 70 * '='? 70 =’s! | https://realpython.com/courses/python-print/continue/#t=487.82 |
| I like this because it gives me a nice little divider I can put on the output. | https://realpython.com/courses/python-print/continue/#t=493.13 |
| 08:17 | https://realpython.com/courses/python-print/continue/#t=497.18 |
| Simple things amuse me. In the next lesson, | https://realpython.com/courses/python-print/continue/#t=497.18 |
| I’ll be talking about the different ways to format strings in Python. | https://realpython.com/courses/python-print/continue/#t=500.51 |
| Aug. 1, 2020 | https://realpython.com/courses/python-print/continue/#comment-4de771e6-7b95-4577-a894-15da27bca507 |
| Aug. 1, 2020 | https://realpython.com/courses/python-print/continue/#comment-c93b24fb-b1db-49fe-8f81-7fc6c06dbb98 |
| Aug. 1, 2020 | https://realpython.com/courses/python-print/continue/#comment-503b09c8-28ac-49b9-a627-43d6fb0fec45 |
| Aug. 1, 2020 | https://realpython.com/courses/python-print/continue/#comment-50f238a1-ed07-46d1-b4f2-7056fed06895 |
| Aug. 2, 2020 | https://realpython.com/courses/python-print/continue/#comment-10fa6f63-46fc-47e6-a775-56a3320bba3e |
| Aug. 3, 2020 | https://realpython.com/courses/python-print/continue/#comment-edb28fd4-d2b2-4c87-b586-b8968b54327e |
| Aug. 10, 2024 | https://realpython.com/courses/python-print/continue/#comment-826903c2-d3e2-4e70-a050-0877df438829 |
| Dec. 28, 2024 | https://realpython.com/courses/python-print/continue/#comment-c953df76-57b5-44bf-8dc1-f47313fe34e4 |
| Dec. 28, 2024 | https://realpython.com/courses/python-print/continue/#comment-904818af-03dc-4ee8-9f7a-4e55f2582480 |
| Dec. 30, 2024 | https://realpython.com/courses/python-print/continue/#comment-822dedb9-477e-4507-8371-12067c6aac9f |
| Become a Member | https://realpython.com/account/join/ |
| Overview | https://realpython.com/courses/python-print/ |
| https://realpython.com/videos/formatting/ |
|
Basic Usage and String Literals 08:25
| https://realpython.com/videos/basic-usage-and-string-literals/ |
|
String Formatting Styles 03:42
| https://realpython.com/videos/formatting/ |
|
sep, end, and flush 07:23
| https://realpython.com/videos/sep-end-and-flush/ |
|
Printing to File Streams 02:43
| https://realpython.com/lessons/printing-file-streams/ |
|
Custom Data Types 04:56
| https://realpython.com/lessons/custom-data-types/ |
|
Python 2's print vs Python 3's print() 01:50
| https://realpython.com/lessons/python-2s-print-vs-python-3s-print/ |
|
Pretty Print 03:27
| https://realpython.com/lessons/pretty-print/ |
|
ANSI Escape Sequences 01:57
| https://realpython.com/lessons/ansi-escape-sequences/ |
|
Animation 04:12
| https://realpython.com/lessons/animation/ |
|
Mocking print() in Unit Tests 05:42
| https://realpython.com/lessons/mocking-print-unit-tests/ |
|
Debugging 10:40
| https://realpython.com/lessons/debugging/ |
|
The Python print() Function (Quiz) 12:00
| https://realpython.com/lessons/the-python-print-function-quiz/ |
|
Related Information 02:05
| https://realpython.com/lessons/related-information/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover