What is the point of .format in python?
Hello! In this article, we will be focusing on formatting string and values using Python format() function. Python format() function is an in-built String function used for the purpose of formatting of strings. The Python Syntax: Example: Output: Index formatting with Python format()The format() function also serves the purpose of formatting string at user defined positions i.e. we can change the position of the string or value to be placed in the output by specifying the index values inside the curly braces. Example: s1 = 'Python' s2 = 'with' s4 = 'Data Science' res = "{2} {1} {0}".format(s1, s2, s4) print(res) In the above snippet of code, the format(s1, s2, s4) internally assigns them index values as 0, 1, 2, and so on to the values passed to the function. That is, s1 is assigned to index 0, s2 is assigned to index 1 and s4 is assigned to index 2. So, by passing the index value of the string in the {}, we can alter the position of the string by the index values. Output: Passing values to the arguments in format() functionUsing Python format() function, we can assign values to the variables we want to be displayed inside the parameter list of the function itself. Syntax: "{var1}".format(var1='value') Example: res = "{s4} {s2} {s1}".format(s1 = 'Python',s2 = 'with',s4 = 'Data Science') print(res) Output: Padding a formatted string with Python format() functionThe strings can also be formatted in terms of the alignment and padding using the format() function. Syntax: #left padding "{:>number}".format(value) #right padding "{: |