format() String Method in Python with Examples
Syntax: format() String Method
S.format(*args, **kwargs) -> str
In the above syntax -> str
represents this method finally returns a string.
format() Syntax Explanation
format()
method returns a formatted version of (string) S, using substitutions from args and kwargs. The substitutions are identified by braces ('{' and '}').
Example 1: format() String Method
name = "Jack"
age = 20
new_string = '{} is {} years old.'.format(name, age)
print(new_string)
Output
Jack is 20 years old.
Example 2: format() String Method
name = "Jack"
age = 20
new_string = '{0} is {1} years old.'.format(name, age)
print(new_string)
Output
Jack is 20 years old.
Example 3: format() String Method
name = "Jack"
age = 20
new_string = '{1} is {0} years old.'.format(name, age)
print(new_string)
Output
20 is Jack years old.
Related String Methods