python format string padding code example
Example 1: python format float
>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True
Example 2: padding in python
MyString = "ABC"
print("'" + MyString.ljust(5) + "'")
print("'" + MyString.rjust(5) + "'")
Output:
'ABC '
' ABC'
Example 3: python format strings
print(f"string {adjustable_input_1} more string {adjustable_input_2}")
name = "Joe Bob"
age = 23
print(f"My name is {name} and I am {age} years old")
--> My name is Joe Bob and I am 23 years old
Example 4: print python format
'{} {}'.format('one', 'two')