How to use string formatting techniques in Python

王林
Release: 2023-10-16 09:39:30
Original
724 people have browsed it

How to use string formatting techniques in Python

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:

  1. String insertion:
name = "Alice"
age = 20
print("My name is %s and I am %d years old." % (name, age))
Copy after login

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.

  1. Floating point formatting:
price = 19.99
print("The price is %.2f dollars." % price)
Copy after login

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.

  1. Other common usages:
num1 = 10
num2 = 3
print("%d + %d = %d" % (num1, num2, num1 + num2))
Copy after login

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:

  1. String insertion:
name = "Bob"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
Copy after login

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.

  1. Specify the insertion position:
name = "Charlie"
age = 30
print("My name is {1} and I am {0} years old.".format(age, name))
Copy after login

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.

  1. Specify format:
price = 9.99
print("The price is {:.2f} dollars.".format(price))
Copy after login

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:

  1. String insertion:
name = "David"
age = 35
print(f"My name is {name} and I am {age} years old.")
Copy after login

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.

  1. Execution expression:
num1 = 5
num2 = 2
print(f"{num1} + {num2} = {num1 + num2}")
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template