Trong bài viết này, chúng ta sẽ tìm hiểu những gì là các chuỗi trong ngôn ngữ lập trình, cách tạo chúng và sử dụng của chúng. Hơn nữa, chúng tôi sẽ nghiên cứu các nhà khai thác khác nhau để so sánh các chuỗi trong Python. Cuối cùng, chúng tôi sẽ nghiên cứu một số so sánh chuỗi Python cùng với ví dụ và đầu ra mã Python của nó. Vậy hãy bắt đầu! Show
Nội dung chính ShowShow
Chuỗi là gì?Toán tử so sánh chuỗi Python Toán tử so sánh chuỗi PythonChuỗi bằng kiểm tra trong Python
Chuỗi bằng kiểm tra trong PythonĐiều gì về trường hợp so sánh không nhạy cảm? Example: s1 = 'String' s2 = 'String' s3 = 'string' # case sensitive equals check if s1 == s2: print('s1 and s2 are equal.') if s1.__eq__(s2): print('s1 and s2 are equal.') So sánh chuỗi trong Python là gì? Bạn có thể sử dụng == cho các chuỗi trong Python không? 6 toán tử so sánh trong Python là gì? 6 toán tử so sánh trong Python là gì? Điều gì về trường hợp so sánh không nhạy cảm?So sánh chuỗi trong Python là gì? s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison') Bạn có thể sử dụng == cho các chuỗi trong Python không? string string 6 toán tử so sánh trong Python là gì? string string 6 toán tử so sánh trong Python là gì? STRING STRING 6 toán tử so sánh trong Python là gì? Điều gì về trường hợp so sánh không nhạy cảm?So sánh chuỗi trong Python là gì? Bạn có thể sử dụng == cho các chuỗi trong Python không? 6 toán tử so sánh trong Python là gì?Using Relational Operators Bạn có thể sử dụng các toán tử so sánh trên chuỗi không?Unicode values of the characters of the strings from the zeroth index till the end of the string. It then returns a boolean value according to the operator used. Example:
Python3==: Toán tử này kiểm tra xem hai chuỗi có bằng nhau không.: This operator checks whether two strings are equal. s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')1 True True False False0 True True False False1 True True False False2 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')5 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')6 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')1 True True False False0 True True False False1 True True False False2 True True False False1 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')6 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')1 True True False False0 True True False False1 True True False False2! =: Toán tử này kiểm tra xem hai chuỗi không bằng nhau.This operator checks whether two strings are not equal. Output: True True False False Phương pháp 2: Sử dụng IS và khôngUsing is and is not Toán tử == so sánh các giá trị của cả hai toán hạng và kiểm tra công bằng giá trị. Trong khi đó, toán tử kiểm tra xem cả hai toán hạng có đề cập đến cùng một đối tượng hay không. Điều tương tự là trường hợp! = Và không.== operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not. The same is the case for != and is not.== operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not. The same is the case for != and is not. Hãy cho chúng tôi hiểu điều này với một ví dụ: Python3s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')21 True True False False3 True True False False2 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')24 True True False False3 True True False False2 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')27 True True False False3 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')29 True True False False0 True True False False1 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')62 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')63 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')64 True True False False1 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')66 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')67 True True False False0 True True False False1 True True False False00 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')63 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')64 True True False False1 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')66 True True False False05 True True False False0 True True False False1 True True False False08 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')63 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')64 True True False False1 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')66 True True False False13 True True False False0 True True False False15 True True False False16 True True False False17 True True False False0 True True False False15 True True False False16 True True False False21 True True False False0 True True False False15 True True False False16 True True False False25 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')21 True True False False277____8 True True False False29 True True False False30 True True False False3 True True False False32 True True False False0 True True False False1 True True False False35 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')63 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')64 True True False False1 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')66 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')67 True True False False0 True True False False1 True True False False33 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')63 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')64 True True False False1 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')66 True True False False38 True True False False0 True True False False15 True True False False16 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')02 Output: s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')2 ID đối tượng của các chuỗi có thể thay đổi trên các máy khác nhau. ID đối tượng của STR1, STR2 và STR3 là như nhau do đó chúng là kết quả đúng trong tất cả các trường hợp. Sau khi ID đối tượng của STR1 được thay đổi, kết quả của STR1 và STR2 sẽ sai. Ngay cả sau khi tạo str4 với cùng nội dung như trong str1 mới, câu trả lời sẽ sai vì ID đối tượng của chúng là khác nhau. Ngược lại sẽ xảy ra với không. Phương pháp 3: Tạo chức năng do người dùng xác định. Creating a user-defined function. Creating a user-defined function. Bằng cách sử dụng các toán tử quan hệ, chúng ta chỉ có thể so sánh các chuỗi bằng các mã hóa của chúng. Để so sánh hai chuỗi theo một số tham số khác, chúng ta có thể tạo các hàm do người dùng xác định. Trong mã sau, chức năng do người dùng xác định của chúng tôi sẽ so sánh các chuỗi dựa trên số lượng chữ số. Python3s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')03 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')04 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')05 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')06 True True False False3 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')08 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')05 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')10 True True False False3 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')08 Is s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')21 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')22 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')23 True True False False3 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')25 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')26 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')27__ s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')31 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')06 True True False False277____8 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')35 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')05 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')14 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')15 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')16 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')17____________ s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')19 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')43 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')21 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')22 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')46 True True False False3 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')25 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')26 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')50 True True False False3 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')31 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')10 True True False False277____8 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')35 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')05 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')60 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')06 True True False False3 True True False False3 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')64 True True False False0 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')66 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')67 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')63 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')69 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')70 True True False False0 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')66 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')69 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')63 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')75 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')70 True True False False0 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')66 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')79 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')63 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')81 s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')70 Output: s1 = 'String' s2 = 'String' s3 = 'string' if s1.casefold() == s3.casefold(): print(s1.casefold()) print(s3.casefold()) print('s1 and s3 are equal in case-insensitive comparison') if s1.lower() == s3.lower(): print(s1.lower()) print(s3.lower()) print('s1 and s3 are equal in case-insensitive comparison') if s1.upper() == s3.upper(): print(s1.upper()) print(s3.upper()) print('s1 and s3 are equal in case-insensitive comparison')6 So sánh chuỗi trong Python là gì?So sánh chuỗi Python được thực hiện bằng cách sử dụng các ký tự trong cả hai chuỗi. Các ký tự trong cả hai chuỗi được so sánh từng cái một. Khi các ký tự khác nhau được tìm thấy thì giá trị unicode của chúng được so sánh. Ký tự có giá trị unicode thấp hơn được coi là nhỏ hơn.performed using the characters in both strings. The characters in both strings are compared one by one. When different characters are found then their Unicode value is compared. The character with lower Unicode value is considered to be smaller.performed using the characters in both strings. The characters in both strings are compared one by one. When different characters are found then their Unicode value is compared. The character with lower Unicode value is considered to be smaller. Bạn có thể sử dụng == cho các chuỗi trong Python không?Chuỗi bằng cách kiểm tra trong Python trong lập trình Python, chúng tôi có thể kiểm tra xem các chuỗi có bằng hay không sử dụng các trò chơi == hay hay bằng cách sử dụng các trò chơi hay không.__eq__ hàm.Ví dụ: S1 = 'String' S2 = 'String' S3 = 'String' # Case Sensitive bằng kiểm tra nếu S1 == S2: In ('S1 và S2 bằng nhau. 6 toán tử so sánh trong Python là gì?Chúng tôi có sáu trong số này, bao gồm và giới hạn nhỏ hơn, lớn hơn, nhỏ hơn hoặc bằng, lớn hơn hoặc bằng, bằng, và không bằng.Vì vậy, hãy bắt đầu với các nhà khai thác so sánh Python.less than, greater than, less than or equal to, greater than or equal to, equal to, and not equal to. So, let's begin with the Python Comparison operators.less than, greater than, less than or equal to, greater than or equal to, equal to, and not equal to. So, let's begin with the Python Comparison operators. Bạn có thể sử dụng các toán tử so sánh trên chuỗi không?Các nhà khai thác so sánh cũng làm việc trên các chuỗi.Để xem liệu hai chuỗi có bằng nhau không, bạn chỉ cần viết biểu thức boolean bằng toán tử bình đẳng.. To see if two strings are equal you simply write a boolean expression using the equality operator.. To see if two strings are equal you simply write a boolean expression using the equality operator. |