Add input and output for code examples (#18646)
This commit is contained in:
@ -8,22 +8,42 @@ The use of f-string allows the programmer to dynamically insert a variable into
|
|||||||
|
|
||||||
To perform these dynamic behaviours within an f-string we wrap them inside curly brackets within the string, and prepend a lower case f to the beginning of the string (before the opening quote.
|
To perform these dynamic behaviours within an f-string we wrap them inside curly brackets within the string, and prepend a lower case f to the beginning of the string (before the opening quote.
|
||||||
|
|
||||||
### Examples
|
## Examples
|
||||||
1. Dynamically inserting a variable into a string at runtime:
|
### Dynamically inserting a variable into a string at runtime:
|
||||||
|
|
||||||
|
#### Input
|
||||||
|
|
||||||
```python
|
```python
|
||||||
name = 'Jon Snow'
|
name = 'Jon Snow'
|
||||||
greeting = f'Hello! {name}'
|
greeting = f'Hello! {name}'
|
||||||
print(greeting)
|
print(greeting)
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Evaluate an expression in a string:
|
#### Output
|
||||||
|
|
||||||
|
```
|
||||||
|
Hello! Jon Snow
|
||||||
|
```
|
||||||
|
|
||||||
|
### Evaluate an expression in a string:
|
||||||
|
|
||||||
|
#### Input
|
||||||
|
|
||||||
```python
|
```python
|
||||||
val1 = 2
|
val1 = 2
|
||||||
val2 = 3
|
val2 = 3
|
||||||
expr = f'The sum of {val1} + {val2} is {val1 + val2}'
|
expr = f'The sum of {val1} + {val2} is {val1 + val2}'
|
||||||
print(expr)
|
print(expr)
|
||||||
```
|
```
|
||||||
3. Calling a function and inserting output within a string:
|
|
||||||
|
#### Output
|
||||||
|
```
|
||||||
|
The sum of 2 + 3 is 5
|
||||||
|
```
|
||||||
|
|
||||||
|
### Calling a function and inserting output within a string:
|
||||||
|
|
||||||
|
#### Input
|
||||||
```python
|
```python
|
||||||
def sum(*args):
|
def sum(*args):
|
||||||
result = 0
|
result = 0
|
||||||
@ -34,7 +54,14 @@ To perform these dynamic behaviours within an f-string we wrap them inside curly
|
|||||||
func = f'The sum of 3 + 5 is {sum(3, 5)}'
|
func = f'The sum of 3 + 5 is {sum(3, 5)}'
|
||||||
print(func)
|
print(func)
|
||||||
```
|
```
|
||||||
4. Joining the contents of a collection within a string:
|
|
||||||
|
#### Output
|
||||||
|
```
|
||||||
|
The sum of 3 + 5 is 8
|
||||||
|
```
|
||||||
|
### Joining the contents of a collection within a string:
|
||||||
|
|
||||||
|
#### Input
|
||||||
|
|
||||||
```python
|
```python
|
||||||
fruits = ['Apple', 'Banana', 'Pear']
|
fruits = ['Apple', 'Banana', 'Pear']
|
||||||
@ -42,5 +69,11 @@ To perform these dynamic behaviours within an f-string we wrap them inside curly
|
|||||||
list_str = f'List of fruits: {", ".join(fruits)}'
|
list_str = f'List of fruits: {", ".join(fruits)}'
|
||||||
print(list_str)
|
print(list_str)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Output
|
||||||
|
```
|
||||||
|
List of fruits: Apple, Banana, Pear
|
||||||
|
```
|
||||||
|
|
||||||
### 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