Python String Formatting
Python string formatting is a powerful feature that allows you to create dynamic strings by combining variables, values, and expressions. It provides a way to format and manipulate strings in a more concise and readable manner.
Using the % Operator
One of the ways to format strings in Python is by using the % operator. This operator allows you to replace placeholders in a string with values from variables or expressions. The placeholders are represented by special formatting codes, such as %s for strings, %d for integers, and %f for floating-point numbers.
Let’s look at some examples:
name = "John"
age = 25
height = 1.8
# String formatting using % operator
greeting = "Hello, my name is %s. I am %d years old and %.2f meters tall." % (name, age, height)
print(greeting)
In the above example, we have defined three variables: name, age, and height. We then use the % operator to format the string “Hello, my name is %s. I am %d years old and %.2f meters tall.” by replacing the placeholders with the corresponding values from the variables. The %.2f formatting code is used to specify that the height should be displayed with two decimal places.
The output of the above code will be:
Hello, my name is John. I am 25 years old and 1.80 meters tall.
Using the format() Method
Another way to format strings in Python is by using the format() method. This method provides a more flexible and readable approach to string formatting.
Here’s an example:
name = "Jane"
age = 30
height = 1.65
# String formatting using format() method
greeting = "Hello, my name is {}. I am {} years old and {:.2f} meters tall.".format(name, age, height)
print(greeting)
In the above example, we use the format() method to format the string “Hello, my name is {}. I am {} years old and {:.2f} meters tall.” by replacing the curly braces {} with the corresponding values from the variables. The {:.2f} syntax is used to specify that the height should be displayed with two decimal places.
The output of the above code will be:
Hello, my name is Jane. I am 30 years old and 1.65 meters tall.
F-Strings (Formatted String Literals)
Python 3.6 introduced a new way to format strings called f-strings or formatted string literals. F-strings provide a concise and readable syntax for string formatting.
Here’s an example:
name = "Alex"
age = 35
height = 1.75
# String formatting using f-strings
greeting = f"Hello, my name is {name}. I am {age} years old and {height:.2f} meters tall."
print(greeting)
In the above example, we use f”…” syntax to create an f-string. The curly braces {} are used to enclose expressions that will be replaced with their values. The {:.2f} syntax is used to specify that the height should be displayed with two decimal places.
The output of the above code will be:
Hello, my name is Alex. I am 35 years old and 1.75 meters tall.
Conclusion
Python string formatting provides a convenient way to create dynamic strings by combining variables, values, and expressions. Whether you choose to use the % operator, the format() method, or f-strings, string formatting allows you to produce more readable and concise code.
By using the examples provided above, you can easily format your strings in Python and make your code more efficient and expressive. Experiment with different formatting codes and syntax to achieve the desired output for your specific use cases.