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 commit8f498480ce
. * Revert "Upgraded node image to latest stable, v8.12 in docker compose" This reverts commite55dd116d2
. * feat: fixed header formatting
This commit is contained in:
committed by
Christopher McCormack
parent
d86951f9c1
commit
ada34f00c5
@ -75,6 +75,7 @@ print(func)
|
|||||||
```
|
```
|
||||||
The sum of 3 + 5 is 8
|
The sum of 3 + 5 is 8
|
||||||
```
|
```
|
||||||
|
|
||||||
### Joining the contents of a collection within a string:
|
### Joining the contents of a collection within a string:
|
||||||
|
|
||||||
#### Input
|
#### Input
|
||||||
@ -90,9 +91,10 @@ print(list_str)
|
|||||||
```
|
```
|
||||||
List of fruits: Apple, Banana, Pear
|
List of fruits: Apple, Banana, Pear
|
||||||
```
|
```
|
||||||
|
|
||||||
### Convert an integer to 8-bit binary
|
### Convert an integer to 8-bit binary
|
||||||
|
|
||||||
### Input
|
#### Input
|
||||||
|
|
||||||
```python
|
```python
|
||||||
num = 42
|
num = 42
|
||||||
@ -100,10 +102,34 @@ num = 42
|
|||||||
print(f'The binary of {num} is {num:08b}')
|
print(f'The binary of {num} is {num:08b}')
|
||||||
```
|
```
|
||||||
|
|
||||||
### Output
|
#### Output
|
||||||
```
|
```
|
||||||
The binary of 42 is 00101010
|
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
|
### Sources
|
||||||
https://www.python.org/dev/peps/pep-0498/
|
https://www.python.org/dev/peps/pep-0498/
|
||||||
|
Reference in New Issue
Block a user