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

Commit

Permalink
Merge pull request #128 from salmccarty/salmac86/add_python_fstrings
Browse files Browse the repository at this point in the history
[Hacktoberfest] Add Python 3 f-Strings snippets
  • Loading branch information
rickwest authored Oct 30, 2019
2 parents c2a40b3 + 34a0875 commit b813161
Showing 1 changed file with 39 additions and 0 deletions.
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

0 comments on commit b813161

Please sign in to comment.