how to use format python code example
Example 1: formatted string python
name = "Foo"
age = 12
print(f"Hello, My name is {name} and I'm {age} years old.")
Example 2: python .format
Name = 'Tame Tamir'
Age = 14
Formatted_string = 'Hello, my name is {name}, I am {age} years old.'.format(name=Name,age=Age)
print(Formatted_string)
Example 3: formatting in python
>>> name = "John"
>>> age = 19
>>> language = "python"
>>> print(f"{name} of age {age} programs in {language}")
John of age 19 programs in python
>>> print("%s of age %d programs in %s" %(name, age, language))
John of age 19 programs in python
>>> print("{} of age {} programs in {}".format(name, age, language))
John of age 19 programs in python
>>> print("{2} of age {1} programs in {0}".format(name, age, language))
python of age 19 programs in John
Example 4: python format strings
name = "Rick"
age = 42
print(f"My name is {name} and I am {age} years old")
Example 5: print python format
print('{} {}'.format(1, 2))
Example 6: How to use .format in python
new_string = "{} is string 1 and {} is string 2".format("fish", "pigs")