thinkcspy
thinkcspy copied to clipboard
Chapter 9 String Interpolation
The PEP standards include two methods for string interpolation. See https://www.python.org/dev/peps/pep-3101/
Python currently provides two methods of string interpolation:
The '%' operator for strings.
- The string.Template module which uses .format like
"My name is {0}".format('Fred')
- The '%' operator for strings like:
"My name is %s" % ('Fred')
But perhaps we should be presenting the simpler syntax of f-strings as described in https://www.python.org/dev/peps/pep-0498/ as recommended by @bnmnetp below?
How many should we show. In my opinion we should be moving toward the future. In fact my preferred method is to teach students f-strings
name = ‘Fred’
print(f”my name is {name}”)
The % operator is still good/interesting to show students as it maps to C/C++/Java but it is definitely not mainstream Python in 2021.
Very fair point. I have updated my Issue.
I am working on this issue
@bnmnetp @pearcej Is the suggestion that we change everywhere the format method is used to using an f-string or only change Activity: 9.5.1.2 ActiveCode?
I think students should be exposed to all of the various string interpolation types so that they understand what they see when they run across code. But I think we should isolate .format and the old printf style formats to a section and then use fstrings consistently throughout.
I have been convinced that @bnmnetp is right about this.