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

Add Tutorial for Python F-Strings #787

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Contributors
- qwong95
- AddaxSoft
- derco0n
- Kjentleman
- ...<perhaps you>...

<p>This project is supported by:</p>
Expand Down
2 changes: 1 addition & 1 deletion static/js/datacamp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$(function() {
var template = "<div data-datacamp-exercise data-lang=\"python\" data-height=\"{{height}}\" data-impact-tracking-link=\"\/c\/67577\/1012793\/13294\"><code data-type=\"sample-code\">{{code}}</div></div>",
var template = "<div data-datacamp-exercise data-lang=\"python\" data-lang-version=\"3.6\" data-height=\"{{height}}\" data-impact-tracking-link=\"\/c\/67577\/1012793\/13294\"><code data-type=\"sample-code\">{{code}}</div></div>",
LINEHEIGHT = 14,
EXTRA = 100;

Expand Down
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
<script src="/static/js/bootbox.min.js"></script>

{% if domain_data.language == "python" and domain_data.language_code == 'en' %}
<script type="text/javascript" src="//cdn.datacamp.com/dcl-react.js.gz"></script>
<script type="text/javascript" src="//cdn.datacamp.com/dcl/latest/dcl-react.js.gz"></script>
<script src="/static/js/datacamp.js"></script>
{% endif %}

Expand Down
2 changes: 1 addition & 1 deletion templates/index-python.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h1>{{ page_title }}</h1>

<!-- DataCamp-Light exercise -->
{% if page_title %}
<div data-datacamp-exercise data-lang="python" data-height="400" data-impact-tracking-link="/c/67577/1012793/13294">
<div data-datacamp-exercise data-lang="python" data-lang-version="3.6" data-height="400" data-impact-tracking-link="/c/67577/1012793/13294">
<code data-type="pre-exercise-code"></code>
<code data-type="sample-code">{{tutorial_data.code}}</code>
<code data-type="solution">{{tutorial_data.solution}}</code>
Expand Down
63 changes: 63 additions & 0 deletions tutorials/learnpython.org/en/Literal String Interpolation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Tutorial
--------

In Python 3.6 another way of formatting strings was introdcued called Literal String Interpolation or more commonly known as f-strings due to the f character that proceeds the string.

String interpolation is the process of replacing the placeholders in the string literal with their corresponding values. This is the same as the C-style formatting from the previous tutorial where the placeholders of %s, %d, etc. are replaced with the values provided after the string literal.

# This prints out "John is 23 years old."
name = "John"
age = 23

print("%s is %d years old." % (name, age))

%s is a placeholder for name and %d is a placeholder for age. When the string is interpolated the values of name and age replace the %s and %d symbols.

F-strings makes string interpolation easier by placing the desired interpolated value in-line with the string inside of curly brackets {}. So instead of `"Hello, %s" % name`, we simply use `f"Hello, {name}"`

# This also prints out "John is 23 years old."
name = "John"
age = 23

print(f"{name} is {age} years old.")

You may also have noticed this eliminates the need to specify the value's type, as f-strings handle this for you.

A poweful benefit of F-strings is that they allow you execute expressions or arthmetic inside the curly brackets.

# This prints out "2 * 6 = 12"
a = 2
b = 6

print(f"{a} * {b} = {a * b}")

Exercise
--------

Rewrite the code using f-strings to produce the following output:
`Hello John Doe. You have 14 fruit.`

Tutorial Code
-------------

first_name = "John"
last_name = "Doe"
apples = 10
pears = 4

# Rewrite this print statement using f-strings
print("Hello %s %s. You have %d apples." % (first_name, last_name, apples))

Expected Output
---------------
test_output_contains("Hello John Doe. You have 14 fruit.", no_output_msg= "Remember to add the `f` before the string and compute the total number of fruit.")
success_msg('Great work!')

Solution
--------
first_name = "John"
last_name = "Doe"
apples = 10
pears = 4

print(f"Hello {first_name} {last_name}. You have {apples + pears} fruit.")
1 change: 1 addition & 0 deletions tutorials/learnpython.org/en/Welcome.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Just click on the chapter you wish to begin from, and follow the instructions. G
- [[Lists]]
- [[Basic Operators]]
- [[String Formatting]]
- [[Literal String Interpolation]]
- [[Basic String Operations]]
- [[Conditions]]
- [[Loops]]
Expand Down