{"id":25552,"date":"2021-11-23T08:57:02","date_gmt":"2021-11-23T03:27:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=25552"},"modified":"2021-11-23T08:57:02","modified_gmt":"2021-11-23T03:27:02","slug":"python-string-format-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-format-method-with-examples\/","title":{"rendered":"Python String format() Method with Examples"},"content":{"rendered":"
String format() Method in Python:<\/strong><\/p>\n The format() method formats the specified value(s) and inserts them inside the placeholder of the string.<\/p>\n Curly brackets{} are used to define the placeholder. In the Placeholder section, you can learn more about the placeholders.<\/p>\n format() returns the formatted string.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n value1, value2, ……. :<\/strong><\/p>\n Required. One or more values to be formatted and inserted into the string<\/p>\n The values are either a comma-separated list of values, a key=value list, or a combination of the two.<\/p>\n Any data type can be used for the values.<\/p>\n For Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Placeholders<\/strong><\/p>\n The placeholders can be identified using named indexes such as {cost} numbered indexes such as 0 {0}, or even empty placeholders {}.<\/p>\n For Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Types of Formatting<\/strong><\/p>\n :<<\/strong>\u00a0 \u00a0 \u00a0 \u00a0 – The result is aligned left (within the available space)<\/p>\n :>\u00a0 \u00a0 \u00a0 \u00a0 – The result is aligned right (within the available space)<\/p>\n :^\u00a0 \u00a0 \u00a0 \u00a0 – The result is centered (within the available space)<\/p>\n :=\u00a0 \u00a0 \u00a0 \u00a0 – Positions the sign to the far left.<\/p>\n :+\u00a0 \u00a0 \u00a0 \u00a0 – To indicate whether the outcome is positive or negative, use a plus sign.<\/p>\n :-\u00a0 \u00a0 \u00a0 \u00a0 – Only use a minus sign for negative values.<\/p>\n :\u00a0 \u00a0 \u00a0 \u00a0 \u00a0– Insert an extra space before positive numbers by using a space (and a minus sign before negative numbers)<\/p>\n :,\u00a0 \u00a0 \u00a0 \u00a0 \u00a0– As a thousand separator, use a comma.<\/p>\n :_\u00a0 \u00a0 \u00a0 \u00a0– As a thousand separator, use an underscore.<\/p>\n :b\u00a0 \u00a0 \u00a0 \u00a0–\u00a0 binary format<\/p>\n :c\u00a0 \u00a0 \u00a0 \u00a0 – The value is converted into the corresponding Unicode character.<\/p>\n :d\u00a0 \u00a0 \u00a0 \u00a0 \u00a0– decimal format<\/p>\n :e\u00a0 \u00a0 \u00a0 \u00a0 \u00a0– Scientific notation in lower case e<\/p>\n :E\u00a0 \u00a0 \u00a0 \u00a0 – Scientific notation in upper case E<\/p>\n :f\u00a0 \u00a0 \u00a0 \u00a0 \u00a0– Fix point number format<\/p>\n :F\u00a0 \u00a0 \u00a0 \u00a0 – Fix point number format, all uppercase (show inf and nan as INF and NAN)<\/p>\n :g\u00a0 \u00a0 \u00a0 \u00a0 \u00a0– General format<\/p>\n :G\u00a0 \u00a0 \u00a0 \u00a0 – Format in General (using an upper case E for scientific notations)<\/p>\n \ud83d\ude2e\u00a0 \u00a0 \u00a0 \u00a0 – octal format<\/p>\n \ud83d\ude21\u00a0 \u00a0 \u00a0 \u00a0 \u00a0-Hexadecimal, lower case<\/p>\n :X\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 -Hexadecimal, upper case<\/p>\n :n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 – format for numbers<\/p>\n :%\u00a0 \u00a0 \u00a0 \u00a0 \u00a0– Format in percentages<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" String format() Method in Python: The format() method formats the specified value(s) and inserts them inside the placeholder of the string. Curly brackets{} are used to define the placeholder. In the Placeholder section, you can learn more about the placeholders. format() returns the formatted string. Syntax: string.format(value1, value2, …….) Parameters value1, value2, ……. : Required. …<\/p>\nstring.format(value1, value2, .......)<\/pre>\n
\n
# Apply the placeholder for the cost variable(static string)and store it in a variable.\r\ngvn_str = \"The dress cost is {cost:.2f} Rs.\"\r\n# Apply the format() function for the above string by passing the cost as some\r\n# random number and print it.\r\nprint(gvn_str.format(cost=850))\r\n<\/pre>\n
The dress cost is 850.00 Rs.<\/pre>\n
\n
# Apply the placeholder for the ename and id (static string) and apply the format()\r\n# function for the given string by passing the ename and id with some random values\r\n# and print it.\r\n# (This is in named indexes format)\r\ngvn_str1 = \"The Employee name is {ename}, and his id is {id}\".format(\r\n ename=\"virat\", id=28)\r\n# Similarly, do the same by numbered indexes format and empty placeholders format.\r\n# Print the results separately.\r\ngvn_str2 = \"The Employee name is {0}, and his id is {1}\".format(\"virat\", 28)\r\n# empty placeholders format\r\ngvn_str3 = \"The Employee name is {}, and his id is {}\".format(\"virat\", 28)\r\nprint(gvn_str1)\r\nprint(gvn_str2)\r\nprint(gvn_str3)\r\n<\/pre>\n
The Employee name is virat, and his id is 28\r\nThe Employee name is virat, and his id is 28\r\nThe Employee name is virat, and his id is 28<\/pre>\n