String Formatting in Python3
In Python 3, there are several ways to format strings, including:
- The % operator This method of string formatting uses the % operator to replace parts of a string with values. For example:
name = "Rahul" age = 30 print("My name is %s and I am %d years old." % (name, age))
- str.format() method This method of string formatting uses the format() method on strings to replace parts of the string with values. For example:
name = "Rahul" age = 30 print("My name is {} and I am {} years old.".format(name, age))
- f-strings f-strings are a newer method of string formatting introduced in Python 3.6. They are a more readable and efficient way to format strings. For example:
name = "Rahul" age = 30 print(f"My name is {name} and I am {age} years old.")
You can use any of the above methods to format strings in Python 3, but the most recommended method is to use f-strings because they are more readable and efficient.
String formatting can be use for string or numbers.
here is the example to demonstrate string formatting -
:<
Left Aligns Output Of String Formatting
str_ = 'Hello python {:<5} programmer'
print(str_.format(40)) # output - Hello python 40 programmer
F-String Formatting
In python, f-string is very important string formatting. Using this string formatting we can style the string like c-language . we can add variables within the string as placeholder using { }
brackets.
x = 10
str_ = f"Hello python {x+10} programmer"
print(str_) # output - Hello python 20 programmer
{:B}
Represent Binary Numbers
Using {:b}
, we can represent a number in binary format in a string .
str_ = "In binary 7 is {:b}"
print(str_.format(7)) # In binary 7 is 111
Represent Numbers In Hex Format
{:x}
we can represent the number in string as hex value .
str_ = "In binary 7 is {:x}"
print(str_.format(7)) # In hex 7 is 7
Represent In Decimal Format
we can format the other types of numbers in decimal .
str_ = "In Decimal 101 is {:d}"
print(str_.format(0b101)) # In Decimal 101 is 5
{:,}
Number Format In String
we can format the number as follow - every thousand places seperated by comma . we can use other seperater .
str_ = "This is big {:,} number"
print(str_.format(38939879289999)) # output- This is big 38,939,879,289,999 number
String Percentage Format
we can represent number in Percentage in a string .
str_ = "you have learn core python {:0%}"
print(str_.format(0.6)) # output - you have learn core python 60.000000%
str_ = "you have learn core python {:.0%}"
print(str_.format(0.6)) # output - you have learn core python 60%
String Formatting Symbols
:< | Left aligns output of string formatting with spaces |
:> | Right aligns output of string formatting with spaces |
:^ | Center aligns output of string formatting with spaces |
:= | Align the sign to the left most position |
:+ | add plus sign for repersent positive value |
:- | add minus sign for represent negative value |
: | Add extra space for positive value and add minus sign for negative value |
:, | Adding comma as a thousand separator |
:_ | Adding underscore as a thousand separator |
:b | Represent Binary format |
:d | Represent Decimal format |
:e | Scientific notations Represent in lowercase 'e' |
:E | Scientific notations Represent in lowercase 'E' |
:f | Represent number in point lowercase format as 'nf' and 'nan' |
:F | Represent number in point, in uppercase format as 'INF' and 'NAN' |
:g | Used for General format for lowercase notations |
:G | General format for uppercase notations |
:o | Octal format notations |
:x | Hex format notations in lower case |
:X | Hex format notations in upper case |
:n | General Number format |
:% | Percentage format for numbers |