From 6361c48b99d2b6858a821e523fc3bf1303455cd7 Mon Sep 17 00:00:00 2001 From: Domingo Moronta Date: Sun, 12 May 2019 15:36:27 -0700 Subject: [PATCH] Add formatted string literals example (#33192) --- .../index.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/guide/english/python/converting-integer-to-string-in-python/index.md b/guide/english/python/converting-integer-to-string-in-python/index.md index db08f67dae..c1d32204d6 100644 --- a/guide/english/python/converting-integer-to-string-in-python/index.md +++ b/guide/english/python/converting-integer-to-string-in-python/index.md @@ -39,6 +39,20 @@ print("Hello, I am " + str(age) + " years old") ``` Run code on repl.it +Formatted string literals or "f-strings": + +Python 3.6 (and later) allows strings to be [formatted with minimal syntax](https://docs.python.org/3/whatsnew/3.6.html#pep-498-formatted-string-literals). + +```py +name = "Xochitl" +age = 45 + +f'Hello, my name is {name}, and I am {age} years young.' + +# Output +# 'Hello, my name is Xochitl, and I am 45 years young.' +``` + Print `1 2 3 4 5 6 7 8 9 10` using a single string ```py result = "" @@ -67,7 +81,9 @@ print(result) #### More Information: -Official Python documentation for `str()` +- Official Python documentation for `str()` + +- Official Python documentation for _formatted string literals_ or _f-strings_.