Quick Introduction to Python String format() Types

In This Article You Will Learn About Python string Format () Types

Python string format () types – Before moving ahead, let’s know a little bit about the Python format () method

format () method – format () method is used to set the format or how to display the text as per your requirement.

Syntax – string.format(value1, value2,…)

Parameter Values

Placeholder – It is denoted from {} – curly brackets, which takes the value as a parameter.

value1 & value2 – It is the required value that will be inserted between the text in place of placeholders. It can be string, variable, and number, etc. In other words, the value can be of any data type.

Now let’s get started with Python Formatting Types –

Formatting Types – A specific formatting type in Python is used with the Python format () method to format the text.

  • Insert formatting type inside placeholder to formatted the text.

1. :<   — It sets the space for some characters from the left side.

Example – Insert the number 15 to make space for 15 characters from the left side. 

Name = "My name is {:<15}."
print(Name.format('Alex'))
Python-string-format-()-types

2. :>   — It sets the space for several characters from the right side.

Example – Insert the number 15 to make space for 15 characters from the right side. 

Name = "My name is {:>15}." 
print(Name.format('Alex'))
Python-string-format-()-types

3. :^  —  It sets the space for several characters in the center.

Name = "My name is {:^15}."
print(Name.format('Alex'))
Python-string-format-()-types

4. :=  —  It places the plus or minus sign at the left-most position. Used number to make available space several characters.

temp = "Yesterday night temperature was {:=10} degrees Fahrenheit." print(temp.format(-8))
Python-string-format-()-types

5. :+  —- It shows if the number is positive or negative.

temp = "Yesterday night temperature was {:+} and {:+} degrees Fahrenheit."
print(temp.format(-8, 4))
Python-string-format-()-types

6. :-  —- It shows if the number is only negative.

temp = "Yesterday night temperature was between {:-} and {:-} degrees Fahrenheit."
print(temp.format(-8, 4))
Python-string-format-()-types

7. :  —— It sets an extra space before positive and negative numbers.

temp = "Yesterday night temperature was between {: } and {: } degrees Fahrenheit."
print(temp.format(-8, 4))
Python-string-format-()-types

8. :, ——- It sets thousands of separators. 

statue = 'This staute was created {:,} years ago.'
print(statue.format(150000))
Python-string-format-()-types

9. :_ ——– It sets thousands of separators. 

statue = 'This staute was created {:_} years ago.'
print(statue.format(150000))

10. :b  —— It converts numbers into binary format.

number = "The binary version of {0} is {0:b}."
print(number.format(12))
Python-string-format-()-types

11. :e —– It converts a number into a scientific number with lower case.

Example – Using formatted type ‘e’ to convert a number into a scientific number.

statue = 'This statue was created {:e} years ago.'
print(statue.format(1500))
Python-string-format-()-types

12. :E —– It converts a number into a scientific number with upper case.

Example – Using formatted type ‘e’ to convert a number into a scientific number.

statue = 'This statue was created {:E} years ago.'
print(statue.format(1500))
Python-string-format-()-types

13. :f —- It fixes the decimal after the particular number. By default, is 6 decimals. But by using numbers, several decimals can be fixed.    

Example – Using formatted type ‘d’ to convert a number into a fixed-point number.

euro = "I have {:f} Euro."
print(euro.format(10))
Python-string-format-()-types

Example – Using formatted type ‘d’ to convert a number into a fixed-point number by taking a number as a parameter to specify points between numbers.

euro = "I have {:.2f} Euro."
print(euro.format(10))
Python-string-format-()-types

14. : o —- It converts a number into an octal number.

Example — Using formatted type ‘o’ to convert a number into an octal number.

number = "The octal version of {0} is {0:o}."
print(number.format(12))
Python-string-format-()-types

15. : x —- It converts a number into a Hex number.

Example – Using formatted type ‘x’ to convert a number into Hex number.

number = "The Hexadecimal version of {0} is {0:x}"
print(number.format(23))
Python-string-format-()-types

16. :% — It converts a number into % format.

Example – Using formatted type ‘%’ to convert a number into a percentage number.

number = "I got 90 percent out of {:.0%}."
print(number.format(1))
Python-string-format-()-types

Example – Using formatted type ‘%’ to convert a number into a percentage number with a decimal.

number = "I got 90 percent out of {:%}."
print(number.format(1))
Python-string-format-()-types

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Leave a Comment

Stay in the loop

codingstreets