How to use string formatting techniques in Python
In Python programming, string formatting is a very important technique. It allows us to process strings more flexibly, insert variables into the string, or specify a specific format for the string. This article will introduce commonly used string formatting methods in Python and provide specific code examples.
1. Use the percent sign (%) for string formatting
The most commonly used string formatting method in Python is to use the percent sign (%) for formatting. The following are some common string formatting syntax:
name = "Alice" age = 20 print("My name is %s and I am %d years old." % (name, age))
The output result is: My name is Alice and I am 20 years old.
In the above code, we use %s and %d as placeholders to insert name and age into the string respectively.
price = 19.99 print("The price is %.2f dollars." % price)
The output result is: The price is 19.99 dollars.
In the above code, we use %.2f Format a floating point number into a string with two decimal places.
num1 = 10 num2 = 3 print("%d + %d = %d" % (num1, num2, num1 + num2))
The output result is: 10 3 = 13
In the above code, we can use the plus sign to combine the variable with To concatenate strings, you can also use the equal sign to concatenate variables with variables.
2. Use braces ({}) for string formatting
In addition to using percent signs for string formatting, Python also provides another string formatting method. , use curly braces for formatting. Here are some examples of string formatting using braces:
name = "Bob" age = 25 print("My name is {} and I am {} years old.".format(name, age))
The output is: My name is Bob and I am 25 years old.
In the above code, we use curly brackets as placeholders and insert name and age into the string through the format() function.
name = "Charlie" age = 30 print("My name is {1} and I am {0} years old.".format(age, name))
The output result is: My name is Charlie and I am 30 years old.
In the above code, we The insertion position of name and age in the string is specified by the serial number.
price = 9.99 print("The price is {:.2f} dollars.".format(price))
The output result is: The price is 9.99 dollars.
In the above code, we use {:.2f} Format a floating point number into a string with two decimal places.
3. Use f-string for string formatting
In Python 3.6 and above, a new string formatting method called f-string is introduced. f-string uses the prefix "f" and inserts the variable directly into the string. Here are some examples of string formatting using f-string:
name = "David" age = 35 print(f"My name is {name} and I am {age} years old.")
The output is: My name is David and I am 35 years old.
In the above code, we write the variable name directly within the curly brackets in the string.
num1 = 5 num2 = 2 print(f"{num1} + {num2} = {num1 + num2}")
The output result is: 5 2 = 7
In the above code, we can write directly within the curly brackets Enter an expression and return the calculated result.
Summary:
This article introduces commonly used string formatting methods in Python, including using percent signs, braces and f-string for string formatting. These methods allow us to process strings more flexibly and insert variables into the string in a specified format. In actual Python programming, we can choose the appropriate string formatting method to use according to the specific situation.
The above is the detailed content of How to use string formatting techniques in Python. For more information, please follow other related articles on the PHP Chinese website!