Why does one work but two doesn’t?
1: language = request.POST.get('form-' + str(i) + '-language')
2: text = request.POST.get('form-%s-text'.format(i))
The format function takes a different set of variable specifiers - not “%s”.
#2 should be:
text = request.POST.get('form-{0}-text'.format(i))
Oh, %s is for the print() function. Thank you!
The string.format() is one of the string formatting methods in Python3, which allows multiple substitutions and value formatting.
Python 3.6 added the new string formatting way called formatted string literals or “f-strings”.
This new method of formatting strings lets you use the embedded Python expressions inside string constants. Here’s a simple example to give you a feel for the feature.
You can use either str.format() or f-strings to format the string.
So, you can use the new string formatting way called formatted string literals or “f-strings”.