A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://realpython.com/lessons/string-formatting-f-strings/ below:

String Formatting With F-Strings (Video) – Real Python

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

String Formatting With F-Strings

Learn about what Python’s formatted string literals (f-strings) are and when to use them. You’ll see that f-strings allow you to use embedded Python expressions inside string constants:

This lesson will also show you how to use f-strings for:

00:01 Moving on to the third major way of formatting strings: f-strings. f-strings was released in Python 3.6 and it sort of streamlined the way that we format strings now.

00:15 So, when should we use f-strings? As I mentioned, Python 3.6 and later versions support f-strings. And, when it’s not a string generated by a user of your program.

00:28 Let’s hop back into the scenario. Once again, we have the two variables, errno and name, that we would like to format into a single string. With f-strings, you just need to prefix the string with the letter f and embed the expressions you want within curly brackets.

00:48 That looks like this. Follow along with me. That’s it! And give it an exclamation point because we’re happy to see Bob, 'Hello, Bob!' This method of formatting also allows you to embed arithmetic into your strings.

01:08 Give this example a try. We’re going to give it two variables, a and b. a = 5, b = 10, and then we’re going to set up our f-string.

01:24 f'Five plus ten is {a + b} and not {2 * (a + b)}', and close that off, and print this out. 'Five plus ten is 15 and not 30.' f-strings also support the formatting or selection of output, so give that a try with me.

02:01 And when it’s time to select hexadecimal, we’re going to do :#x.

02:14 'Hey Bob, there is a 0xbadc0ffee error!' So, you can see here how f-strings kind of streamline and make string formatting just a little bit simpler. Less characters to type, and it’s quicker to set up where you want to substitute your variables.

Course Contents


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4