1- Introduction Software Engineering
Format strings are a way to inject variables into a string in Python. They are used to format strings and produce more human-readable outputs. There are several ways to format strings in Python:
Introduced in Python 3.6, f-strings are a new way to format strings in Python. They are prefixed with 'f' and use curly braces {} to enclose the variables that will be formatted. For example:
123name = "John"age = 30print(f"My name is {name} and I am {age} years old.")Copied!
This will output:
1My name is John and I am 30 years old.Copied!
This is another way to format strings in Python. It uses curly braces {} as placeholders for variables which are passed as arguments in the format() method. For example:
123name = "John"age = 50print("My name is {} and I am {} years old.".format(name, age))
This will output:
1My name is John and I am 50 years old.Copied!
This is one of the oldest ways to format strings in Python. It uses the % operator to replace variables in the string. For example:
123name = "Johnathan"age = 30print("My name is %s and I am %d years old." % (name, age))Copied!
This will output:
1My name is Johnathan and I am 30 years old.Copied!