Hướng dẫn python string concatenation vs fstring - nối chuỗi python vs fstring

Hiệu suất khôn ngoan, tôi đã mong đợi chuỗi định dạng theo nghĩa đen sẽ nhanh hơn đáng kể so với cách ghép chuỗi tuy nhiên tôi đã bị sốc khi thấy đây không phải là trường hợp.

Tôi đã sử dụng mô -đun

For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
0 để kiểm tra thời gian mất bao lâu là chuỗi chữ so với nối chuỗi. Tôi đã kiểm tra các chuỗi có độ dài 10 đến 1 triệu ký tự.

from timeit import timeit
import matplotlib.pyplot as plt
n = 1000000000

setup = """\
a = 'a'*{str_len}
b = 'b'*{str_len}
"""

fstr_stmt = """\
f'{a}{b}'
"""

concat_stmt = """\
a+b
"""

str_lens = [10, 100, 1000, 10000, 100000, 1000000]
fstr_t = []
concat_t = []
for str_len in str_lens:
    n_iters = n//str_len
    fstr_t.append(timeit(setup=setup.format(str_len=str_len), stmt=fstr_stmt, number=n_iters)/n_iters)
    concat_t.append(timeit(setup=setup.format(str_len=str_len), stmt=concat_stmt, number=n_iters)/n_iters)
    ratio = fstr_t[-1]/concat_t[-1]
    print(f"For two strings of length {str_len:7d}, concatenation is {ratio:.5f} times faster than f-strings")
plt.plot(str_lens, fstr_t, "r*-")
plt.plot(str_lens, concat_t, "c*-")
plt.xscale("log")
plt.yscale("log")
plt.xlabel("String length (log scale)")
plt.ylabel("Seconds per iteration (log scale)")
plt.grid()
plt.show()

Đầu ra bảng điều khiển:

For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings

Và cốt truyện:

Hướng dẫn python string concatenation vs fstring - nối chuỗi python vs fstring

Tóm tắt: Sử dụng toán tử nối chuỗi nhanh hơn một chút so với việc sử dụng các chuỗi chữ số định dạng. Trừ khi bạn đang thực hiện hàng trăm ngàn lần ghép chuỗi và cần chúng được thực hiện rất nhanh, việc thực hiện được chọn không có khả năng tạo ra sự khác biệt. Using the string concatenation operator is slightly faster than using format string literals. Unless you are performing many hundreds of thousands of string concatenations and need them done very quickly, the implementation chosen is unlikely to make a difference.

Từ quan điểm dễ đọc, các chữ F-String có tính thẩm mỹ hơn và dễ đọc hơn so với nối dây. Ngoài ra, vì câu trả lời từ Daniel chỉ ra, các chuỗi F có thể xử lý các đầu vào của các loại khác nhau trong khi sử dụng

For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
1 yêu cầu cả hai đối tượng phải là chuỗi (hoặc quá tải các phương thức
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
2 và
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
3).

EDIT: Như Chepner chỉ ra trong nhận xét của họ, sử dụng F-Strings hiệu quả hơn khi có hơn hai chuỗi. Ví dụ: thêm một biến khác,

For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
4, vào các câu lệnh Cài đặt và
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
0 mang lại đầu ra bảng điều khiển sau:
: As chepner points out in their comment, using f-strings is more efficient when more than two strings are involved. For example, adding another variable,
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
4, to the setup and
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
0 statements yields the following console output:

For three strings of length      10, concatenation is 0.77931 times faster than f-strings
For three strings of length     100, concatenation is 0.67699 times faster than f-strings
For three strings of length    1000, concatenation is 0.60220 times faster than f-strings
For three strings of length   10000, concatenation is 1.27484 times faster than f-strings
For three strings of length  100000, concatenation is 0.98911 times faster than f-strings
For three strings of length 1000000, concatenation is 0.60201 times faster than f-strings

Đăng nhập vào tài khoản Python Barsels của bạn để lưu cài đặt screencast của bạn.

Vẫn chưa có tài khoản? Đăng ký tại đây.

Hãy nói về cách xây dựng các chuỗi lớn hơn từ các chuỗi nhỏ hơn trong Python. Hai phương pháp để thực hiện điều này là nối chuỗi và nội suy chuỗi.string concatenation and string interpolation.

Kết hợp chuỗi

Chúng tôi đã có một chuỗi đại diện cho một tên

For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
6 và chúng tôi sẽ tạo một chuỗi mới
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
7. Chúng tôi đang thực hiện điều này bằng cách sử dụng ký hiệu
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
1 giữa chuỗi,
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
9 và chuỗi
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
6:

>>> name = "Trey"
>>> message = "My name is " + name
>>> message
'My name is Trey'

Điều này được gọi là nối chuỗi. Từ nối về cơ bản là một từ lạ mắt để dán mọi thứ lại với nhau. Vì vậy, khi chúng tôi kết nối hai chuỗi, chúng tôi đang dán chúng lại với nhau bằng cách sử dụng dấu

For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
1.string concatenation. The word concatenation is basically just a fancy word for gluing things together. So when we're concatenating two strings, we're gluing them together by using the
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
1 sign.

Bây giờ, kết hợp chuỗi là tuyệt vời, nhưng đôi khi nó có thể có một chút khó sử dụng.

Đây là một ví dụ dài hơn nhiều về sự kết hợp chuỗi:

>>> "My name is " + name + " which has " + len(name) + " characters."

Thật dễ dàng để làm lỗi chính tả, trong khi bạn đang sử dụng kết nối chuỗi vì bạn đã có báo giá và cộng tất cả các xen kẽ giữa nhau. Cuối cùng chúng tôi đã không tạo một lỗi đánh máy ở trên, nhưng chúng tôi có lỗi trong mã của chúng tôi.

>>> "My name is " + name + " which has " + len(name) + " characters."
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

Gọi

For three strings of length      10, concatenation is 0.77931 times faster than f-strings
For three strings of length     100, concatenation is 0.67699 times faster than f-strings
For three strings of length    1000, concatenation is 0.60220 times faster than f-strings
For three strings of length   10000, concatenation is 1.27484 times faster than f-strings
For three strings of length  100000, concatenation is 0.98911 times faster than f-strings
For three strings of length 1000000, concatenation is 0.60201 times faster than f-strings
2 trả về một số nguyên. Mặc dù chúng ta có thể sử dụng
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
1 để thêm số nguyên vào số nguyên và chúng ta có thể sử dụng
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
1 để kết hợp một chuỗi với chuỗi, chúng ta không thể sử dụng
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
1 giữa một chuỗi và số nguyên.

Vì vậy, chúng ta cần chuyển đổi

For three strings of length      10, concatenation is 0.77931 times faster than f-strings
For three strings of length     100, concatenation is 0.67699 times faster than f-strings
For three strings of length    1000, concatenation is 0.60220 times faster than f-strings
For three strings of length   10000, concatenation is 1.27484 times faster than f-strings
For three strings of length  100000, concatenation is 0.98911 times faster than f-strings
For three strings of length 1000000, concatenation is 0.60201 times faster than f-strings
2 thành một chuỗi để kết hợp tất cả các chuỗi này với nhau:

>>> "My name is " + name + " which has " + str(len(name)) + " characters."
'My name is Trey which has 4 characters.'

Mã mà chúng tôi đã kết thúc là mã Python khá dễ đọc, nhưng nó có thể dễ đọc hơn.

Chuỗi nội suy

Một cách khác để xây dựng một chuỗi lớn hơn từ các chuỗi nhỏ hơn là nội suy chuỗi. Trong Python, điều này thường được gọi là định dạng chuỗi. Để thực hiện định dạng chuỗi, chúng ta có thể sử dụng chuỗi F.string interpolation. In Python this is often called string formatting. To do string formatting, we can use an f-string.

Một chuỗi F không phải là công cụ duy nhất cho điều này (phương thức chuỗi ____27 là phương pháp khác), nhưng nó là công cụ phổ biến nhất bạn sẽ thấy:

>>> f"My name is {name} which has {len(name)} characters."
'My name is Trey which has 4 characters.'

Một chuỗi F là một chuỗi có "F" ở phía trước của nó. Không có "F" đó ở phía trước của nó, nó chỉ là một chuỗi thông thường:

>>> "My name is {name} which has {len(name)} characters."
'My name is {name} which has {len(name)} characters.'

Chỉ đơn giản bằng cách đặt F F ngay trước chuỗi, bất kể chúng tôi sử dụng dấu ngoặc kép

For three strings of length      10, concatenation is 0.77931 times faster than f-strings
For three strings of length     100, concatenation is 0.67699 times faster than f-strings
For three strings of length    1000, concatenation is 0.60220 times faster than f-strings
For three strings of length   10000, concatenation is 1.27484 times faster than f-strings
For three strings of length  100000, concatenation is 0.98911 times faster than f-strings
For three strings of length 1000000, concatenation is 0.60201 times faster than f-strings
8 hay trích dẫn đơn
For three strings of length      10, concatenation is 0.77931 times faster than f-strings
For three strings of length     100, concatenation is 0.67699 times faster than f-strings
For three strings of length    1000, concatenation is 0.60220 times faster than f-strings
For three strings of length   10000, concatenation is 1.27484 times faster than f-strings
For three strings of length  100000, concatenation is 0.98911 times faster than f-strings
For three strings of length 1000000, concatenation is 0.60201 times faster than f-strings
9, Python sẽ tìm kiếm niềng răng xoăn bên trong chuỗi chúng tôi đã viết và nó sẽ thực hiện bất cứ thứ gì bên trong các niềng răng xoăn đó (
>>> name = "Trey"
>>> message = "My name is " + name
>>> message
'My name is Trey'
0), lấy bất kỳ đối tượng nào trở lại từ quá trình thực thi mã đó, và sau đó chuyển đổi nó thành một chuỗi nếu cần (như với
For three strings of length      10, concatenation is 0.77931 times faster than f-strings
For three strings of length     100, concatenation is 0.67699 times faster than f-strings
For three strings of length    1000, concatenation is 0.60220 times faster than f-strings
For three strings of length   10000, concatenation is 1.27484 times faster than f-strings
For three strings of length  100000, concatenation is 0.98911 times faster than f-strings
For three strings of length 1000000, concatenation is 0.60201 times faster than f-strings
2). Tiếp theo, nó sẽ dán các chuỗi nhỏ bên trong chuỗi lớn hơn của chúng tôi, về cơ bản là đưa chúng vào chuỗi lớn hơn của chúng tôi:

>>> f"My name is {name} which has {len(name)} characters."
'My name is Trey which has 4 characters.'

Bản tóm tắt

Bạn có thể nghĩ về sự kết hợp chuỗi như dán dây lại với nhau. Và, bạn có thể nghĩ về phép nội suy chuỗi mà không cần các chuỗi tiêm bên trong các chuỗi khác.string interpolation without strings as injecting strings inside of other strings.

Hai cách để xây dựng một chuỗi lớn hơn từ các chuỗi nhỏ hơn là sự kết hợp chuỗi (sử dụng ký hiệu

For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
1) và nội suy chuỗi (định dạng chuỗi a.k.a.) sử dụng các chuỗi F.string concatenation (using the
For two strings of length      10, concatenation is 1.06938 times faster than f-strings
For two strings of length     100, concatenation is 1.14887 times faster than f-strings
For two strings of length    1000, concatenation is 1.13994 times faster than f-strings
For two strings of length   10000, concatenation is 1.26934 times faster than f-strings
For two strings of length  100000, concatenation is 1.21585 times faster than f-strings
For two strings of length 1000000, concatenation is 1.01816 times faster than f-strings
1 symbol
) and string interpolation (a.k.a. string formatting) which uses f-strings.

Cách tốt nhất để nối các chuỗi trong Python là gì?

Một trong những phương pháp phổ biến nhất để kết hợp hai chuỗi trong Python (hoặc nhiều hơn) là sử dụng toán tử +. Toán tử +, khi được sử dụng với hai chuỗi, kết hợp các chuỗi với nhau để tạo thành một.using the + operator. The + operator, when used with two strings, concatenates the strings together to form one.

Sự khác biệt giữa nối chuỗi và nội suy là gì?

Bạn có thể nghĩ về sự kết hợp chuỗi như dán các chuỗi với nhau. Và, bạn có thể nghĩ về phép nội suy chuỗi mà không cần các chuỗi khi tiêm các chuỗi bên trong các chuỗi khác.string concatenation as gluing strings together. And, you can think of string interpolation without strings as injecting strings inside of other strings.

Chuỗi F có nhanh hơn Concatenate không?

Tóm tắt: Sử dụng toán tử nối chuỗi nhanh hơn một chút so với việc sử dụng các chuỗi chữ số định dạng.Trừ khi bạn đang thực hiện hàng trăm ngàn lần ghép chuỗi và cần chúng được thực hiện rất nhanh, việc thực hiện được chọn không có khả năng tạo ra sự khác biệt.Using the string concatenation operator is slightly faster than using format string literals. Unless you are performing many hundreds of thousands of string concatenations and need them done very quickly, the implementation chosen is unlikely to make a difference.

Những lợi ích của việc sử dụng phương thức .Format thay vì nối chuỗi trong Python là gì?

Ưu điểm chính của việc sử dụng định dạng (Mạnh) là chuỗi có thể dễ dàng hơn một chút để sản xuất và đọc như cụ thể trong ví dụ thứ hai và chúng ta không phải chuyển đổi rõ ràng tất cả các biến không chuỗi thành chuỗi bằng str (…).the string can be a bit easier to produce and read as in particular in the second example, and that we don't have to explicitly convert all non-string variables to strings with str(…).