Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added f-string samples to string-formatting #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions tutorial/string-formatting.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Formatting strings and output
==============================
Examples in Python 3

str.format() in Python 3

```python
"""
Expand Down Expand Up @@ -31,8 +30,25 @@ print("{0:10}{1:10}".format("Hello", "World"))


```
f-string formatting in Python 3
```python
"""
Python 3.6 added support for new kind of formatting.
"""
# The syntax is similar to the one you used with str.format() but less verbose and also faster.
name = "John"
f"Hello, {name}"

# F-strings are evaluated at runtime, you can put any and all valid Python expressions in them.
f"1 + 1 = {1+1}"

# You could also call functions
def to_lowercase(string):
return string.lower()

f"My name is {to_lowercase("John")} at lower-case"

```

Reference and read more
------------------------------
Expand Down