Python is a powerful programming language that offers a wide range of features and functionalities. One of the most important features of Python is its string formatting capabilities. String formatting is the process of creating a formatted string by replacing placeholders with values. In Python, string formatting is achieved through a variety of methods, including the % operator, the format() method, and f-strings.
Python string formatting allows you to create formatted strings by replacing placeholders with values. The placeholders can be in the form of curly braces {} or percent signs %, depending on the method used. The values can be passed as arguments to the formatting method or operator, or they can be accessed from variables or other sources.
The % operator is one of the oldest and most widely used methods of string formatting in Python. It works by replacing placeholders with values using a format string that specifies the type and format of the values. For example, the following code uses the % operator to format a string:
name = "John"
age = 30
print("My name is %s and I am %d years old." % (name, age))
The output of this code will be:
My name is John and I am 30 years old.
The format() method is a newer and more flexible method of string formatting in Python. It works by replacing placeholders with values using a format string that specifies the type and format of the values. For example, the following code uses the format() method to format a string:
name = "John"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
The output of this code will be:
My name is John and I am 30 years old.
The f-string is the newest and most concise method of string formatting in Python. It works by embedding expressions inside curly braces {} in a string literal. For example, the following code uses an f-string to format a string:
name = "John"
age = 30
print(f"My name is {name} and I am {age} years old.")
The output of this code will be:
My name is John and I am 30 years old.
Here are some more examples of Python string formatting using the % operator, the format() method, and f-strings:
# Formatting integers
x = 10
print("The value of x is %d." % x)
# Formatting floats
y = 3.14159
print("The value of pi is %f." % y)
# Formatting strings
name = "John"
print("My name is %s." % name)
# Formatting multiple values
x = 10
y = 20
print("The values of x and y are %d and %d, respectively." % (x, y))
# Formatting integers
x = 10
print("The value of x is {}.".format(x))
# Formatting floats
y = 3.14159
print("The value of pi is {:.2f}.".format(y))
# Formatting strings
name = "John"
print("My name is {}.".format(name))
# Formatting multiple values
x = 10
y = 20
print("The values of x and y are {} and {}, respectively.".format(x, y))
# Formatting integers
x = 10
print(f"The value of x is {x}.")
# Formatting floats
y = 3.14159
print(f"The value of pi is {y:.2f}.")
# Formatting strings
name = "John"
print(f"My name is {name}.")
# Formatting multiple values
x = 10
y = 20
print(f"The values of x and y are {x} and {y}, respectively.")