python conversion specifier code example
Example 1: python string format
string_a = "Hello"
string_b = "Cena"
print("{0}, John {1}"
.format(string_a, string_b))
print("{greeting}, John {last_name}"
.format(greeting=string_a, last_name=string_b))
Example 2: python format specifier
from datetime import datetime
'{:%Y-%m-%d %H:%M}'.format(datetime(2001, 2, 3, 4, 5))
Example 3: python print format
print(f'I love {Geeks} from {Portal}')
Example 4: 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 5: python %d
print ('%s is %d years old' % ('Joe', 42))
>>>Joe is 42 years old
Example 6: %s %d python
name = "Gandalf"
extendedName = "the Grey"
age = 84
IQ = 149.9
print('type(name):', type(name))
print('type(age):', type(age))
print('type(IQ):', type(IQ))
print('%s %s\'s age is %d with incredible IQ of %f ' %(name, extendedName, age, IQ))
print ('{0} {1}\'s age is {2} with incredible IQ of {3} '.format(name, extendedName, age, IQ))
print ('{} {}\'s age is {} with incredible IQ of {} '.format(name, extendedName, age, IQ))
print("Multiplication of %d and %f is %f" %(age, IQ, age*IQ))
sub1 = "python string!"
sub2 = "an arg"
a = "i am a %s" % sub1
b = "i am a {0}".format(sub1)
c = "with %(kwarg)s!" % {'kwarg':sub2}
d = "with {kwarg}!".format(kwarg=sub2)
print(a)
print(b)
print(c)
print(d)