Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

[Hacktoberfest] Add Python 3 f-Strings snippets #128

Merged
merged 1 commit into from
Oct 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions source/snippets/python/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,45 @@ input("Press enter to continue...")

```

---

## Python 3 f-strings: formatted string literals

The new f-Strings are string literals that start with an `f` at the beginning, and allow for expressions to be evaluated inside of curly braces. This is way easier to keep track of, especially with a long set of parameters and strings.

#### Simple Syntax
```python
name = "Sal"
age = 33
f"Hello, {name}. You are {age}"
# 'Hello, Sal. You are 33.'

# Capital `F` is also valid
F"Hello, {name}. You are {age}"
# 'Hello, Sal. You are 33.'

# The output returned is a string, with single quotes, but you can wrap f-Strings in the print command, too.
print(f"{name} is {age} and that is great!")
# Sal is 33 and that is great!
```
#### Arbitrary Expressions
Since f-Strings are evaluated at runtime, you can put any valid Python expression inside of them. This allows for some cool things to happen!
```python
# The basics
f"{2 * 21}"
# '42'

# You can call functions
def to_lowercase(input):
return input.lower()

name = "Sal Mac"
f"{to_lowercase(name)} loves to code."
# 'sal mac loves to code.'

```

---

## Two Matrix Multiplication

Expand Down