126
Python String format() Method
Python format() method is used to perform format operations on string. While formatting string a delimiter {} (braces) is used to replace it with the value. This delimeter either can contain index or positional argument.
Signature
Parameters
- *args : substring
- **kwargs : start index a range
Return Type
It returns a formatted string.
Let’s see some examples to understand the format() method.
Python String format() Method Example 1
An example of simple format method which format string using positional delimiter.
Output:
Java and C# both are programming languages
Python String format() Method Example 2
The delimiter (braces) are using numerical index to replace and format string.
Output:
C# and Java both are programming languages
Python String format() Method Example 3
Formatting numerical value in different-different number systems. See the below example.
Output:
decimal: 10 hex: a octal: 12 binary: 1010
Python String format() Method Example 4
Formating float and percentile in string is pretty easy.
Output:
decimal: 100,000,000 decimal: 622.22%
Next TopicPython Strings