name = "John" age = 30 # F-strings (Python 3.6+) message = f"Hello, {name}! You are {age} years old." print(message) # Output: "Hello, John! You are 30 years old." # str.format() method message = "Hello, {}! You are {} years old.".format(name, age) print(message) # Output: "Hello, John! You are 30 years old." # % operator message = "Hello, %s! You are %d years old." % (name, age) print(message) # Output: "Hello, John! You are 30 years old." # Template strings (Python 2.4+) from string import Template template = Template("Hello, $name! You are $age years old.") message = template.substitute(name=name, age=age) print(message) # Output: "Hello, John! You are 30 years old."
F-strings
F-strings, or formatted string literals, are a concise and convenient way to include the value of expressions inside string literals in Python. They were introduced in Python 3.6 and are denoted by the f prefix and curly braces {} containing the expressions to be formatted.
Here is an example of using an f-string to print a simple string with a variable inserted:
name = "John" print(f"Hello, {name}!") # Output: "Hello, John!"
You can also use format specifiers inside the curly braces to specify how the value should be formatted. For example, to print a floating point number with two decimal places, you can use the :.2f format specifier:
value = 3.14159 print(f"The value is {value:.2f}") # Output: "The value is 3.14" str.format() method
The str.format() method is a more flexible way to format strings in Python. It involves using placeholders {} in a string and calling the format() method on the string, passing in the values to be inserted into the placeholders as arguments.
Here is an example of using the str.format() method to print a string with a variable inserted:
name = "John" print("Hello, {}!".format(name)) # Output: "Hello, John!"
You can also use format specifiers in the placeholders to specify how the values should be formatted. For example, to print a floating point number with two decimal places, you can use the :.2f format specifier:
value = 3.14159 print("The value is {:.2f}".format(value)) # Output: "The value is 3.14"
% operator
The % operator is another way to format strings in Python. It involves using the % operator and a format specifier to specify how the values should be formatted.
Here is an example of using the % operator to print a string with a variable inserted:
name = "John" print("Hello, %s!" % name) # Output: "Hello, John!"
You can also use multiple values in a string by using multiple format specifiers and passing in a tuple of values. For example, to print two strings and a floating point number, you can use the %s and %.2f format specifiers and pass in a tuple of values:
name = "John" value = 3.14159 print("Hello, %s! The value is %.2f" % (name, value)) # Output: "Hello, John! The value is 3.14"
Template strings
Template strings are a way to create strings that contain placeholders that can be replaced with values. They are defined using the Template class from the string module and use the $ symbol as a placeholder.
Here is an example of using a template string to create a string with a variable inserted:
from string import Template name = "John" template = Template("Hello, $name!") print(template.substitute(name=name)) # Output: "Hello, John!"
You can also use format specifiers in the placeholders to specify how the values should be formatted. For example, to create a string with a floating point number formatted with two decimal places, you can use the :.2f format specifier:
from string import Template value = 3.14159 template = Template("The value is $value:.2f") print(template.substitute(value=value)) # Output: "The value is 3.14"
Comparison
Here is a summary of the different string formatting methods discussed in this tutorial:
Each of these methods has its own strengths and limitations, and you can choose the one that best fits your needs. F-strings are generally the most concise and easiest to read, but the str.format() method and % operator may be more familiar to users coming from older versions of Python. Template strings are a more powerful option if you need to create complex strings with many placeholders and values.