Làm thế nào để kết hợp
class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
2 với một chuỗi [nguyên thủy] mà không quá tải và loại đúc rõ ràng [class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
3]?class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
_string = Foo['text'] + 'string'
Output:
Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
Nhà điều hành
class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
4 phải bị quá tải? Có những cách khác [chỉ tự hỏi]?Tái bút: Tôi biết về quá tải các toán tử và loại đúc [như
class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
5]
hỏi ngày 16 tháng 2 năm 2012 lúc 16:03Feb 16, 2012 at 16:03
Tomastomastomas
4412 Huy hiệu vàng5 Huy hiệu bạc13 Huy hiệu đồng2 gold badges5 silver badges13 bronze badges
6
Chỉ cần xác định các phương pháp
class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
6 và class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
7:class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
Chúng sẽ được gọi tùy thuộc vào việc bạn làm
class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
8 [gọi class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
6] hay _string = Foo['text'] + 'string'
0 [gọi class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
7].Đã trả lời ngày 16 tháng 2 năm 2012 lúc 16:08Feb 16, 2012 at 16:08
Sven Marnachsven MarnachSven Marnach
545K114 Huy hiệu vàng914 Huy hiệu bạc816 Huy hiệu đồng114 gold badges914 silver badges816 bronze badges
5
_string = Foo['text'] + 'string'
Vấn đề với dòng này là Python nghĩ rằng bạn muốn thêm
_string = Foo['text'] + 'string'
2 vào một đối tượng loại _string = Foo['text'] + 'string'
3, không phải là cách khác.Nó sẽ hoạt động mặc dù nếu bạn viết:
_string = "%s%s" % [Foo['text'], 'string']
CHỈNH SỬA
Bạn có thể thử nó với
_string = 'string' + Foo['text']
Trong trường hợp này, đối tượng
_string = Foo['text'] + 'string'
3 của bạn phải được tự động được chọn vào một chuỗi.Đã trả lời ngày 16 tháng 2 năm 2012 lúc 16:06Feb 16, 2012 at 16:06
ConstantiniusconstantiniusConstantinius
33.2K7 Huy hiệu vàng74 Huy hiệu bạc84 Huy hiệu đồng7 gold badges74 silver badges84 bronze badges
4
Nếu điều đó có ý nghĩa đối với đối tượng
_string = Foo['text'] + 'string'
3 của bạn, bạn có thể làm quá tải phương thức _string = Foo['text'] + 'string'
6 như sau:class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
_string = Foo['text'] + 'string'
print _string
Ví dụ đầu ra:
textstring
Đã trả lời ngày 16 tháng 2 năm 2012 lúc 16:08Feb 16, 2012 at 16:08
Sven Marnachsven Marnachjcollado
545K114 Huy hiệu vàng914 Huy hiệu bạc816 Huy hiệu đồng8 gold badges102 silver badges133 bronze badges
Kết hợp chuỗi là một hoạt động rất phổ biến trong lập trình. Sự kết hợp chuỗi python có thể được thực hiện bằng cách sử dụng nhiều cách khác nhau. Hướng dẫn này nhằm mục đích khám phá các cách khác nhau để nối các chuỗi trong một chương trình Python.
Sự kết hợp chuỗi python
Chúng ta có thể thực hiện kết nối chuỗi bằng cách sử dụng các cách sau:
- Sử dụng toán tử +
- Sử dụng phương thức tham gia []
- Sử dụng nhà điều hành %
- Sử dụng hàm định dạng []
- Sử dụng chuỗi F [nội suy chuỗi theo nghĩa đen]
Concatenation chuỗi bằng cách sử dụng toán tử +
Đây là cách đơn giản nhất để kết hợp chuỗi. Hãy cùng nhìn vào một ví dụ đơn giản.
s1 = 'Apple'
s2 = 'Pie'
s3 = 'Sauce'
s4 = s1 + s2 + s3
print[s4]
Đầu ra:
_string = Foo['text'] + 'string'
7 Hãy cùng xem xét một ví dụ khác, nơi chúng ta sẽ nhận được hai chuỗi từ đầu vào của người dùng và kết hợp chúng.s1 = input['Please enter the first string:\n']
s2 = input['Please enter the second string:\n']
print['Concatenated String =', s1 + s2]
Output:
Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
0Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
1Chúng ta có thể sử dụng hàm str [] để có được biểu diễn chuỗi của một đối tượng. Hãy cùng xem cách kết hợp một chuỗi vào số nguyên hoặc đối tượng khác.
Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
2Đầu ra:
Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
3Vấn đề lớn nhất với toán tử + là chúng ta có thể thêm bất kỳ dấu phân cách hoặc dấu phân cách nào giữa các chuỗi. Ví dụ: nếu chúng ta phải kết hợp với Hello Hello và thế giới với một dấu cách phân tách khoảng trắng, chúng ta sẽ phải viết nó dưới dạng
_string = Foo['text'] + 'string'
8.Chuỗi nối
Chúng ta có thể sử dụng hàm Jop [] để kết hợp chuỗi với một dấu phân cách. Nó rất hữu ích khi chúng ta có một chuỗi các chuỗi, ví dụ như danh sách hoặc tuple của chuỗi. Nếu bạn không muốn một dấu phân cách, thì hãy sử dụng hàm Jop [] với một chuỗi trống.
Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
4Output:
Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
5Concatenation chuỗi bằng cách sử dụng toán tử %
Chúng ta có thể sử dụng toán tử % cho định dạng chuỗi, nó cũng có thể được sử dụng để nối chuỗi. Nó rất hữu ích khi chúng tôi muốn kết hợp các chuỗi và thực hiện định dạng đơn giản.
Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
6Output:
Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
7Chuỗi kết nối bằng cách sử dụng hàm định dạng []
Chúng ta cũng có thể sử dụng hàm định dạng chuỗi [] để nối chuỗi và định dạng.
Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
8Output:
Traceback [most recent call last]:
File "test.py", line 10, in
_string = Foo['text'] + 'string'
TypeError: unsupported operand type[s] for +: 'type' and 'str'
9Chức năng Python String Format [] rất mạnh, sử dụng nó chỉ để nối các chuỗi không phải là cách sử dụng đúng đắn.
Kết nối chuỗi bằng cách sử dụng chuỗi f
Nếu bạn đang sử dụng Python 3.6+, bạn cũng có thể sử dụng chuỗi F để nối chuỗi. Nó là một cách mới để định dạng chuỗi và được giới thiệu trong PEP 498 - nội suy chuỗi theo nghĩa đen.
class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
0Output:
class Foo:
def __init__[self, text]:
self.text = text
def __str__[self]:
return self.text
def __add__[self, other]:
return str[self] + other
def __radd__[self, other]:
return other + str[self]
1Python F-String sạch hơn và dễ ghi hơn khi so sánh với hàm định dạng []. Nó cũng gọi hàm str [] khi một đối số đối tượng được sử dụng làm thay thế trường.
Sự kết luận
Định dạng chuỗi Python có thể được thực hiện theo nhiều cách. Sử dụng chúng dựa trên yêu cầu của bạn. Nếu bạn phải nối trình tự chuỗi với một hàm được phân định, thì hãy sử dụng hàm nối []. Nếu một số định dạng cũng được yêu cầu với sự kết hợp, thì hãy sử dụng hàm định dạng [] hoặc chuỗi f. Lưu ý rằng chuỗi F có thể được sử dụng với các phiên bản Python 3.6 trở lên.
Bạn có thể kiểm tra toàn bộ tập lệnh Python và nhiều ví dụ về Python từ Kho lưu trữ GitHub của chúng tôi.