In Python 3, print() is now a function, not a statement. This is a major change from earlier versions, and is different from many other languages. print() is also used for formatting strings and data.
- [Instructor] The Python print function is simple and powerful and it's probably a bit different than what you're used to in other languages. Here in Komodo I've opened a working copy of hello.py from chapter two of the exercise files. And you'll notice that down here on line four, I have a print function with a literal string which says hello world. And so if I save and run this, you see the output says hello world. Now if I want to print a variable value in the string, I'll use the format method.
Which is actually a method of the string object. And so I put a placeholder in here with a pair of curly braces. And I say dot format. And I'll put a value up here, I'll make a variable. X equals 42. And I put that x in the format method. And when I save and run, you see now we get a 42 down there in our output. The format method is very powerful, and very flexible and we'll cover it in detail later in the course. But for now you know enough to use it in many many cases.
It's worth noting that this is not a function or a method of the print function. This is a method of the string object itself. And so I can take this whole string object including the format call, and put it on a separate line and say s equals, and print s. And you see we will get exactly the same result when I save and run this. So for now I'm going to put this back the way that we had it.
And save and run, you can see it still works. Now earlier versions of Python like Python two, which if you're familiar with Python two, or if you see legacy code you will see a lot of Python two. It works very differently because print is not a function, and strings are not objects. And so instead, it works more like C, but not entirely. So I would say percent d here. And close the string.
And I would use a percent sign here and then the variable. And so that would replace the placeholder percent d. Which that placeholder is now specifically for an integer value, that d means d for an integer value. Just like it does in the C language. And so when I save this and run this, you see we get exactly the same result. Now this usage, this is used in legacy code that uses Python two. It's deprecated, and it will be removed from future versions of Python.
So instead, you're going to want to always use the format method when you're using Python three. And so I'll put the x there. And I need to replace this with the placeholder. And because my editor is trying to help me, it did that. And so we save and run, and we have now that same result. Now beginning with Python 3.6, this is not in all versions of Python three, just beginning with 3.6 and later. And what we have here is 3.6.3 if I remember correctly.
We can take all this format stuff out, and not call the method like that. And instead, you use what's called an F string. So I put the letter F in front of the string, and then I simply put the x variable inside of the curly braces. And when we save and run, we see that that works exactly the same. So F stands for format, and the F strings are actually implemented by calling the format method. So it has all the relevant functionality from format.
And again, we'll cover this in a lot of detail later in the course. But for right now, this is how interpolation and formatting of strings works in Python three.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.