Python Format with Dictionary Object
more_vert
Python format is commonly used to replace placeholders with variables. The following is an typical example:
a = "100" b = "200" string = "a={a}, b={b}".format(a=a, b=b) print(string)
It outputs a string with this value - "a=100, b=200". In some scenarios, it is very convenient to use a dictionary to format string. This can be done using '**' operator in Python. In other programming languages, this is called splat or spread operator.
Code snippets
dict = {'a': "100", 'b': "100"} string = "a={a}, b={b}".format(**dict) print(string)
The output is the same as the above:
a=100, b=200
copyright
This page is subject to Site terms.
comment Comments
No comments yet.
Log in with external accounts
warning Please login first to view stats information.
article
Write and read parquet files in Python / Spark
article
Extract Value from XML Column in PySpark DataFrame
article
Spark - Read from BigQuery Table
article
PySpark: Convert JSON String Column to Array of Object (StructType) in Data Frame
code
Kafka Consumer - Fetch Records from Beginning
Read more (96)