xxxxxxxxxx
Number Format Output Description
3.1415926 {:.2f} 3.14 Format float 2 decimal places
3.1415926 {:+.2f} +3.14 Format float 2 decimal places with sign
-1 {:+.2f} -1.00 Format float 2 decimal places with sign
2.71828 {:.0f} 3 Format float with no decimal places
5 {:0>2d} 05 Pad number with zeros (left padding, width 2)
5 {:x<4d} 5xxx Pad number with x’s (right padding, width 4)
10 {:x<4d} 10xx Pad number with x’s (right padding, width 4)
1000000 {:,} 1,000,000 Number format with comma separator
0.25 {:.2%} 25.00% Format percentage
1000000000 {:.2e} 1.00e+09 Exponent notation
13 {:10d} 13 Right aligned (default, width 10)
13 {:<10d} 13 Left aligned (width 10)
13 {:^10d} 13 Center aligned (width 10)
xxxxxxxxxx
# Newer f-string format
name = "Foo"
age = 12
print(f"Hello, My name is {name} and I'm {age} years old.")
# output :
# Hello, my name is Foo and I'm 12 years old.
xxxxxxxxxx
# String Formatting
name1 = 'Andrei'
name2 = 'Sunny'
print(f'Hello there {name1} and {name2}') # Hello there Andrei and Sunny - Newer way to do things as of python 3.6
print('Hello there {} and {}'.format(name1, name2))# Hello there Andrei and Sunny
print('Hello there %s and %s' %(name1, name2)) # Hello there Andrei and Sunny --> you can also use %d, %f, %r for integers, floats, string representations of objects respectively
xxxxxxxxxx
string_a = "Hello"
string_b = "Cena"
# Index based:
print("{0}, John {1}"
.format(string_a, string_b))
# Object based:
print("{greeting}, John {last_name}"
.format(greeting=string_a, last_name=string_b))
xxxxxxxxxx
# you want make a counter loop and put that number into the some strings or a string
# you want show client this Message:
#"you are customer number <number>"
# for this task Just write this:
for i in range(10):
print(f"you are customer number{i}")
#DA DA!!!!
# Finally, you were able to hack NASA
# this elementry shits are very important. lern it by right way
#******************************
#And care about women's rights*
#******************************
xxxxxxxxxx
name = "Rick"
age = 42
print(f"My name is {name} and I am {age} years old")
xxxxxxxxxx
>>> nombre = 357568.12312
>>> nombre2 = 568.568768
>>> nombre3 = -34.3432
>>> nombre4 = 23
>>> print(f'{nombre : >+20_.4f} {nombre2 : >+20_.4f}')
>>> print(f'{nombre3 : >+20_.4f} {nombre4 : >+20_.4f}')
+357_568.1231 +568.5688
-34.3432 +23.0000
xxxxxxxxxx
# Basic syntax:
print(f"string {adjustable_input_1} more string {adjustable_input_2}")
# Example usage:
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