
Python format() String Method โ A Complete Guide with Examples
Python format() String Method : The Python format() method is a powerful tool used to replace placeholders in strings with specified values. It provides flexible options for string formatting, including numeric conversions, alignment, and custom templates.
Syntax of Python format() Method
python
template string .format(value1, value2, …)
Python format() String Method Parameters
- value1, value2: Values that replace placeholders in the string. These can be strings, numbers, or variables.
Return Value
The method returns a formatted string with values inserted in place of the placeholders.
Placeholders in format()
- Empty Placeholder ({}): Automatically replaced in sequence.
- Positional Arguments ({0}, {1}): Values are inserted based on their index.
- Keyword Arguments ({name}): Values are inserted based on variable names.
Examples of Python format() Method
1. Using Empty Placeholder
python
print(“Welcome to {} tutorials”.format(“Software Moji Moji”))
Output:
Welcome to Software Moji Moji tutorials
2. Using Positional Arguments
python
print(“Welcome to {0} {1} Tutorials”.format(“Software”, “Moji”))
Output:
Welcome to Software Moji Tutorials
3. Using Keyword Arguments
python
print(“Welcome to {name} {num} Tutorials”.format(name=”Software Moji”, num=”Moji”))
Output:
Welcome to Software Moji Moji Tutorials
4. Multiple Placeholders
python
print(“{} is {} new kind of {} experience!”.format(“Software Moji Moji”, “totally”, “learning”))
Output:
Software Moji Moji is totally new kind of learning experience!
Python format() String Method Formatting Options in format()
Python format() allows various formatting features within placeholders.
1. Decimal Format (:d)
python
print(“The binary to decimal value is : {:d}”.format(0b0011))
Output:
The binary to decimal value is : 3
2. Binary Format (:b)
python
print(“The binary value is : {:b}”.format(500))
Output:
The binary value is : 111110100
3. Scientific Notation (:e, :E)
python
print(“The scientific value is : {:e}”.format(40))
print(“The scientific value is : {:E}”.format(40))
Output:
The scientific value is : 4.000000e+01
The scientific value is : 4.000000E+01
4. Fixed-Point Format (:f)
python
print(“The value is : {:.2f}”.format(40))
Output:
The value is : 40.00
5. Octal Format (๐ฎ)
python
print(“The value is : {:o}”.format(500))
Output:
The value is : 764
6. Hexadecimal Format (๐ก, :X)
python
print(“The value is : {:x}”.format(500))
print(“The value is : {:X}”.format(500))
Output:
The value is : 1f4
The value is : 1F4
7. Percentage Format (:%)
python
print(“The value is : {:.0%}”.format(0.80))
Output:
The value is : 80%
8. Thousand Separator (:,)
python
print(“The value is : {:,}”.format(1000000))
Output:
The value is : 1,000,000
Alignment in format()
- :< (Left Align)
- :> (Right Align)
- :^ (Center Align)
Example:
python
print(“The value {:<10} is positive”.format(40)) # Left align
print(“The value {:>10} is positive”.format(40)) # Right align
print(“The value {:^10} is positive”.format(40)) # Center align
Output:
csharp
The value 40 is positive
The value 40 is positive
The value 40 is positive
Using Class with format()
python
class MyClass:
msg1 = “Software Moji”
msg2 = “Tutorials”
print(“Welcome to {c.msg1} Moji {c.msg2}!”.format(c=MyClass()))
Output:
Welcome to Software Moji Moji Tutorials!
Python format() String Method Using Dictionary with format()
python
my_dict = {‘msg1’: “Welcome”, ‘msg2’: ‘Software Moji Moji’}
print(“{m[msg1]} to {m[msg2]} Tutorials!”.format(m=my_dict))
Output:
Welcome to Software Moji Moji Tutorials!