in this article you will learn about Python format () method
Python format () method – Before moving ahead, let’s know a little bit about Python User Input Function
format method – format () method is used to set the format or how to display the text as per your requirement.
Insert Placeholder (curly brackets {}) between the parts of text you want to set value through format () method.
Placeholder – It defined as indexed of number {0}, indexed of name {name} and empty placeholder {} as well.
Syntax – string. format (values…)
Parameter values-
Value – A string, integer, and variables.
Return Type – It returns a formatted text with given values.
Example 1- Add a Placeholder (curly brackets {}) between the text to display the name.
name = 'My name is {}'. format ('Alex') + '.' print(name)
Given these points that text is returned formatted.
Example 2- Add a Placeholder (curly brackets {}) between the text to display age.
age = 'My age is {}'. format(19) + '.' print(age)
As has been noted, it returned the formatted text.
format () method allows us to add parameters inside the curly bracket to decide how have to show formatted text.
Example 3- Add a Placeholder with format identifier f to convert a number into a decimal number.
number = 'I got score {:.2f}'. format(19) + '.' print(number)
Consequently, it returned the formatted text.
Multiple Values – format () method allows us to add more than one value or multiple values to format the text.
Add as many values you want inside the format () method to format the text.
Example 4- Add Multiple Placeholders between the text to display formatted text.
Bio = 'My name is {}. I am {} years old. I live in {}'. format ('Alex', 19, 'NYC') + '.' print (Bio)
Hence, it returned the formatted text after placing values inside placeholders.
Index Number – By referring index number inside placeholders, you can set any value to anywhere inside text.
Example 5- Add Placeholders with index numbers between the text to display formatted text.
Bio = 'My name is {0}. I am {1} years old. I live in {2}'. format ('Alex', 19, 'NYC') + '.' print (Bio)
As a result, it returned the formatted text after taking values as per index number.
Same Value – To insert the same value in the text, refer to the same index number in more than one placeholder.
Example 6- Add Placeholders with the same index numbers between the text to display the same value in formatted text.
Bio = 'My name is {0}. {0} is {1} years old. {0} live in {2}'. format ('Alex', 19, 'NYC') + '.' print(Bio)
Finally, it returned the formatted text after taking the same values as per the index number.
Name Index – By referring to the variable name, you can set any value to text, but you have to write the variable name inside the placeholder to give value to text while passing parameters.
Example 7- Add Placeholders with variable names between the text to display formatted text.
Bio = 'My name is {name}. I am {age} years old. I live in {place}'. format (name='Alex', age=19, place='NYC') + '.' print(Bio)
Accordingly, to formate () method, values have been placed to the right placeholder hence returned formatted text.
If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.
Like us on