From ada34f00c55f64a5956b373b98c384f7c865554b Mon Sep 17 00:00:00 2001 From: Ruslan Kharitonov Date: Mon, 10 Jun 2019 20:52:01 -0400 Subject: [PATCH] F strings objects example (#30712) * Upgraded node image to latest stable, v8.12 in docker compose * f-strings example with capital letter F * Example of using f-strings with an object * Revert "f-strings example with capital letter F" This reverts commit 8f498480ce786afdf3664cc8c6dd687a0cf4134c. * Revert "Upgraded node image to latest stable, v8.12 in docker compose" This reverts commit e55dd116d2bd8ee69df2f29c9cc6d3feccefa91f. * feat: fixed header formatting --- .../english/python/python-f-strings/index.md | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/guide/english/python/python-f-strings/index.md b/guide/english/python/python-f-strings/index.md index f6ecfed1fc..69449fb60c 100644 --- a/guide/english/python/python-f-strings/index.md +++ b/guide/english/python/python-f-strings/index.md @@ -75,6 +75,7 @@ print(func) ``` The sum of 3 + 5 is 8 ``` + ### Joining the contents of a collection within a string: #### Input @@ -90,9 +91,10 @@ print(list_str) ``` List of fruits: Apple, Banana, Pear ``` + ### Convert an integer to 8-bit binary -### Input +#### Input ```python num = 42 @@ -100,10 +102,34 @@ num = 42 print(f'The binary of {num} is {num:08b}') ``` -### Output +#### Output ``` The binary of 42 is 00101010 ``` +### Using Object with f-strings + +#### Input + +```python +class Book: + def __init__(self, title, author): + self.title = title + self.author = author + + def __str__(self): + return f"{self.title} by {self.author}" + + +book = Book("A Clash of Kings", "George R. R. Martin") + +print(f"{book}") +``` + +##### Output +``` +A Clash of Kings by George R. R. Martin +``` + ### Sources https://www.python.org/dev/peps/pep-0498/