string — Common string operations — Python 3.8.1 ... | python replace library
FormatStringSyntaxThestr.format()[1]methodandtheFormatter[2]classsharethesamesyntaxforformatstrings(althoughinthecaseofFormatter[3],subclassescandefinetheirownformatstringsyntax).Thesyntaxisrelatedtothatofformattedstringliterals[4],butitislesssophisticatedand,inparticular,doesnotsupportarbitraryexpressions.Formatstringscontain“replacementfields”surroundedbycurlybraces{}.Anythingthatisnotcontainedinbracesisconsideredliteraltext,whichiscopiedunchangedtotheoutput.Ifyouneedtoincludeabracecharact...
Format String SyntaxThe str.format()[1] method and the Formatter[2] class share the same syntax for format strings (although in the case of Formatter[3], subclasses can define their own format string syntax). The syntax is related to that of formatted string literals[4], but it is less sophisticated and, in particular, does not support arbitrary expressions.
Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.
The grammar for a replacement field is as follows:
replacement_field ::= "{" [field_name[5]] ["!" conversion[6]] [":" format_spec[7]] "}" field_name ::= arg_name[8] ("." attribute_name[9] | "[" element_index[10] "]")* arg_name ::= [identifier[11] | digit[12]+] attribute_name ::= identifie...