Hướng dẫn does == work with strings in python? - == không hoạt động với chuỗi trong python?

So sánh chuỗi Python có thể được thực hiện bằng cách sử dụng các toán tử bình đẳng (==) và so sánh (,! =, =). Không có phương pháp đặc biệt để so sánh hai chuỗi.

So sánh chuỗi Python

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. Hãy cùng xem qua một số ví dụ để so sánh chuỗi.

fruit1 = 'Apple'

print(fruit1 == 'Apple')
print(fruit1 != 'Apple')
print(fruit1 < 'Apple')
print(fruit1 > 'Apple')
print(fruit1 <= 'Apple')
print(fruit1 >= 'Apple')

Output:

True
False
False
False
True
True

Cả hai chuỗi đều giống hệt nhau, do đó chúng bằng nhau. Vì vậy, toán tử bình đẳng đang trả về đúng trong trường hợp này. Hãy cùng xem xét một ví dụ khác, nơi chúng tôi sẽ nhận được đầu vào từ người dùng và sau đó so sánh chúng.

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")

Output:

Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.

Hãy để xem liệu so sánh có nhạy cảm với trường hợp hay không? Ngoài ra, nếu ’một người đến’ một?

print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))

Output:

False
True
A unicode is 65 ,a unicode is 97

Vì vậy, Apple Apple, nhỏ hơn khi so sánh với Apple Apple vì các giá trị Unicode của chúng. Chúng tôi đang sử dụng hàm ord () để in giá trị điểm mã Unicode của các ký tự. Điều gì sẽ xảy ra nếu một trong các chuỗi được làm bằng chuỗi thứ hai và một số ký tự bổ sung?

print('Apple' < 'ApplePie')

Output:

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
0 If the characters sequence are the same in both the strings but one of them have some additional characters, then the larger length string is considered greater than the other one. What if we use < and > operators to compare two equal strings?

print('apple' < 'apple')
print('apple' > 'apple')

Output:

False
False

Rõ ràng, cả hai chuỗi đều không nhỏ hơn cũng không lớn hơn chuỗi kia. Do đó đầu ra là sai trong cả hai trường hợp.

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.

Đối với tất cả các đối tượng python tích hợp (như chuỗi, danh sách, dicts, chức năng, v.v.), nếu x là y, thì x == y cũng đúng.

Không phải lúc nào cũng vậy. Nan là một ví dụ. Nhưng thông thường, bản sắc (

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
1) ngụ ý sự bình đẳng (
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2). Converse không đúng: hai đối tượng riêng biệt có thể có cùng giá trị.

Ngoài ra, thường được coi là tốt hơn khi chỉ sử dụng '==' theo mặc định, ngay cả khi so sánh các giá trị int hoặc boolean?

Bạn sử dụng

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2 khi so sánh các giá trị và
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
1 khi so sánh danh tính.

Khi so sánh Ints (hoặc các loại bất biến nói chung), bạn luôn luôn muốn cái trước. Có một tối ưu hóa cho phép các số nguyên nhỏ được so sánh với

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
1, nhưng đừng dựa vào nó.

Đối với các giá trị Boolean, bạn không nên thực hiện so sánh gì cả. Thay vì:

if x == True:
    # do something

write:

True
False
False
False
True
True
0

Để so sánh với

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
6,
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
7 được ưu tiên hơn
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
8.

Tôi luôn thích sử dụng 'là' bởi vì tôi thấy nó mang tính thẩm mỹ và pythonic hơn (đó là cách tôi rơi vào cái bẫy này ...), nhưng tôi tự hỏi liệu nó có dự định chỉ dành cho khi bạn quan tâm đến việc tìm kiếm hai các đối tượng có cùng ID.

Vâng, đó chính xác là những gì nó dành cho.

Các chuỗi trong Python được so sánh với các toán tử

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2 và
Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
0. Chúng so sánh nếu hai chuỗi python tương đương hoặc không tương đương, tương ứng. Họ trả lại
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
0 hoặc
Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
2.


Thông thường, khi bạn làm việc với các chuỗi trong Python, bạn có thể muốn so sánh chúng với nhau. Ví dụ: bạn có thể muốn so sánh địa chỉ email của người dùng với địa chỉ bạn đã lưu trữ trong cơ sở dữ liệu khi bạn yêu cầu họ đặt lại mật khẩu của họ.

Hướng dẫn does == work with strings in python? - == không hoạt động với chuỗi trong python?

Tìm Bootcamp của bạn phù hợp

  • Karma nghề nghiệp phù hợp với bạn với bootcamp công nghệ hàng đầu
  • Truy cập các học bổng và khóa học chuẩn bị độc quyền

Chọn mối quan tâm của bạn họ Tên điện thoại Email Tên của bạn
First name

Last name

Email

Phone number

Bằng cách tiếp tục, bạn đồng ý với các điều khoản dịch vụ và chính sách quyền riêng tư của chúng tôi và bạn đồng ý nhận được các ưu đãi và cơ hội từ Karma nghề nghiệp qua điện thoại, tin nhắn văn bản và email.

Python bao gồm một số toán tử so sánh có thể được sử dụng để so sánh các chuỗi. Các toán tử này cho phép bạn kiểm tra cách các chuỗi so sánh với nhau và trả về giá trị đúng hoặc sai dựa trên kết quả.

Hướng dẫn này sẽ thảo luận về các toán tử so sánh có sẵn để so sánh các chuỗi trong Python. Chúng tôi sẽ đi qua một ví dụ về từng nhà khai thác này để cho thấy cách chúng hoạt động và cách bạn có thể sử dụng chúng trong mã của mình. Nếu bạn đang tìm cách học cách so sánh các chuỗi trong Python, bài viết này là dành cho bạn.

Chuỗi python là và không bằng

Chuỗi là chuỗi các ký tự có thể bao gồm số, chữ cái, ký hiệu và khoảng trắng. Chuỗi là một loại dữ liệu quan trọng vì chúng cho phép các lập trình viên tương tác với dữ liệu dựa trên văn bản trong các chương trình của họ.

Khi bạn làm việc với một chuỗi, bạn có thể muốn xem liệu một chuỗi có hay không bằng chuỗi khác. Đó là nơi mà các toán tử so sánh chuỗi

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2 và
Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
0 xuất hiện.

Toán tử bình đẳng

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2 trả về true nếu hai giá trị khớp với nhau; Nếu không, người vận hành trả về sai. Toán tử
Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
0 trả về true nếu hai giá trị không khớp và sai nếu hai giá trị khớp.

Điều quan trọng cần lưu ý là so sánh chuỗi là nhạy cảm trường hợp. Vì vậy, các chữ cái chữ thường và chữ hoa sẽ ảnh hưởng đến kết quả của các so sánh bạn thực hiện trong chương trình Python của bạn.case sensitive. So, lowercase letters and uppercase letters will affect the result of the comparisons you perform in your Python program.

Hãy nói rằng bạn đang xây dựng một trò chơi kiểm tra người chơi về kiến ​​thức về thủ đô của họ. Để kiếm điểm, người chơi phải trả lời chính xác một câu hỏi. Vì vậy, một người chơi có thể được trao cho bang California và để đạt được điểm, họ sẽ cần phải vào rằng thủ đô là Sacramento vào chương trình.

Dưới đây, một ví dụ về ứng dụng trò chơi đoán này so sánh câu trả lời của người dùng với câu trả lời được lưu trữ bởi chương trình:

True
False
False
False
True
True
1

Ở đây, những gì xảy ra khi chúng ta chạy trò chơi đoán của mình và đoán chính xác thủ đô của Del biết là Dover:

True
False
False
False
True
True
2

Các chuỗi của chúng tôi bằng nhau, vì vậy

Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
7 của chúng tôi đánh giá để sửa và in ra
Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
8. Nếu chúng ta đoán không chính xác vốn nhà nước là Denver, mã của chúng ta sẽ trả về:

True
False
False
False
True
True
3

Hãy để chia nhỏ mã của chúng tôi. Trong lần đầu tiên, chúng tôi tuyên bố trạng thái ngẫu nhiên của chúng tôi, trong trường hợp này là Del biết. Sau đó, chúng tôi sử dụng phương thức người dùng

Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
9 để hỏi người dùng
print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))
0.

Chương trình của chúng tôi sau đó tuyên bố vốn nhà nước là Dover và sử dụng tuyên bố

print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))
1 để so sánh liệu vốn nhà nước có được lưu trữ có bằng với những gì người dùng đã nhập hay không.

Khi chúng tôi nhập

print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))
2, câu lệnh IF được đánh giá là TRUE, vì vậy chương trình của chúng tôi đã in tin nhắn
Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
8 lên bảng điều khiển. Khi chúng tôi nhập
print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))
4, câu lệnh của chúng tôi đã đánh giá sai, vì vậy chương trình của chúng tôi đã thực hiện mã trong câu lệnh in
print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))
5.

Python là nhà điều hành

Phương pháp phổ biến nhất được sử dụng để so sánh các chuỗi là sử dụng các toán tử

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2 và
Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
0, so sánh các biến dựa trên các giá trị của chúng. Tuy nhiên, nếu bạn muốn so sánh xem hai trường hợp đối tượng có giống nhau dựa trên ID đối tượng của chúng hay không, thay vào đó bạn có thể muốn sử dụng
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
1 và
print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))
9.

Sự khác biệt giữa

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2 và
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
1 (và
Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
0 và
print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))
9) là toán tử so sánh
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2 so sánh hai biến dựa trên giá trị thực của chúng và từ khóa
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
1 so sánh hai biến dựa trên ID đối tượng của chúng.

Hãy để sử dụng một ví dụ. Nói rằng chúng tôi có điểm của hai người dùng được lưu trữ dưới dạng chuỗi và chúng tôi muốn xem liệu chúng có giống nhau hay không. Chúng tôi có thể làm như vậy bằng cách sử dụng mã sau:

True
False
False
False
True
True
4

Mã của chúng tôi trả về:

True
False
False
False
True
True
5

Trong mã trên, chúng tôi cũng có thể đã sử dụng toán tử

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2. Tuy nhiên, chúng tôi đã sử dụng toán tử
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
1 vì nó sử dụng hết bộ nhớ và chúng tôi chỉ cần so sánh hai đối tượng.

Tuyên bố

False
True
A unicode is 65 ,a unicode is 97
8 được đánh giá là TRUE trong chương trình của chúng tôi vì cả hai biến
False
True
A unicode is 65 ,a unicode is 97
9 và
print('Apple' < 'ApplePie')
0 đều có cùng một ID đối tượng. Chúng tôi có thể kiểm tra các ID này bằng cách sử dụng từ khóa
print('Apple' < 'ApplePie')
1:

True
False
False
False
True
True
6

Mã của chúng tôi trả về:

True
False
False
False
True
True
7

Như bạn có thể thấy, các đối tượng của chúng tôi giống nhau, và do đó, toán tử

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
1 được đánh giá là true. Nói chung, bạn nên sử dụng
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2 khi bạn so sánh các loại dữ liệu bất biến như chuỗi và số, và khi so sánh các đối tượng.

Python các nhà khai thác so sánh khác

Ngoài ra, bạn có thể so sánh các chuỗi theo thứ tự từ vựng bằng Python. Thứ tự từ vựng đề cập đến việc đặt hàng các chữ cái dựa trên thứ tự bảng chữ cái của các chữ cái thành phần của chúng. Để làm như vậy, chúng ta có thể sử dụng các nhà khai thác so sánh khác được cung cấp bởi Python. Đây là như sau:

  • print('Apple' < 'ApplePie')
    
    4 - ít hơn
  • print('Apple' < 'ApplePie')
    
    5 - lớn hơn
  • print('Apple' < 'ApplePie')
    
    6 - ít hơn hoặc bằng
  • print('Apple' < 'ApplePie')
    
    7 - lớn hơn hoặc bằng

Hãy nói rằng chúng tôi đã tạo ra một chương trình lấy hai tên sinh viên và trả lại một tin nhắn có tên đầu tiên trong bảng chữ cái.

Chúng tôi có thể sử dụng mã sau để thực hiện nhiệm vụ này:

True
False
False
False
True
True
8

Mã của chúng tôi trả về:

Hướng dẫn does == work with strings in python? - == không hoạt động với chuỗi trong python?

"Karma nghề nghiệp bước vào cuộc sống của tôi khi tôi cần nó nhất và nhanh chóng giúp tôi kết hợp với bootcamp. Hai tháng sau khi tốt nghiệp, tôi tìm thấy công việc mơ ước của mình phù hợp với các giá trị và mục tiêu của tôi trong cuộc sống!"

Sao Kim, Kỹ sư phần mềm tại Rockbot

True
False
False
False
True
True
9

Hãy để chia nhỏ mã của chúng tôi. Trên hai dòng đầu tiên, chúng tôi tuyên bố hai biến lưu trữ tên sinh viên của chúng tôi. Trong trường hợp này, những cái tên này là Penny và Paul.

Sau đó, chúng tôi tạo ra một câu lệnh IF sử dụng toán tử

print('Apple' < 'ApplePie')
8 để xác định xem tên Penny có đến trước tên Paul Paul theo thứ tự từ vựng hay không. Nếu điều này đánh giá đúng, một thông điệp được in vào bảng điều khiển nói với chúng tôi rằng Penny đến trước Paul trong bảng chữ cái.

Chúng tôi cũng tạo ra một tuyên bố

print('Apple' < 'ApplePie')
9 sử dụng toán tử
print('apple' < 'apple')
print('apple' > 'apple')
0 để xác định xem tên Penny có đến trước tên Paul Paul trong bảng chữ cái hay không. Nếu điều này đánh giá đúng, một thông điệp được in vào bảng điều khiển nói với người dùng rằng Paul đến trước Penny trong bảng chữ cái.

Trong trường hợp này, tên Paul Paul xuất hiện trước Penny, trong bảng chữ cái, vì vậy mã trong khối

print('Apple' < 'ApplePie')
9 của chúng tôi đánh giá là đúng và thông điệp
print('apple' < 'apple')
print('apple' > 'apple')
2. được in vào bảng điều khiển.

Sự kết luận

So sánh hai chuỗi là một tính năng quan trọng của Python. Chẳng hạn, bạn có thể tạo một biểu mẫu đăng nhập cần so sánh mật khẩu mà người dùng đã nhập với mật khẩu họ đã đặt cho tài khoản của họ.

Các toán tử so sánh Python có thể được sử dụng để so sánh các chuỗi trong Python. Các toán tử này là: bằng (

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")
2), không bằng (
Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
0), lớn hơn (
print('Apple' < 'ApplePie')
5), nhỏ hơn (
print('Apple' < 'ApplePie')
4), nhỏ hơn hoặc bằng (
print('Apple' < 'ApplePie')
6) và lớn hơn hoặc bằng (
print('Apple' < 'ApplePie')
7). Hướng dẫn này đã khám phá cách các nhà khai thác này có thể được sử dụng để so sánh các chuỗi và đi qua một vài ví dụ về so sánh chuỗi trong Python.

Bây giờ bạn đã sẵn sàng để bắt đầu so sánh các chuỗi trong Python như một Pro!

Có thể == được sử dụng với chuỗi không?

Bạn không nên sử dụng == (toán tử bình đẳng) để so sánh các chuỗi này vì chúng so sánh tham chiếu của chuỗi, tức là chúng có cùng một đối tượng hay không.Mặt khác, phương thức bằng () so sánh liệu giá trị của các chuỗi có bằng nhau hay không là chính đối tượng. because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

Chúng ta có thể so sánh hai chuỗi bằng cách sử dụng == trong Python không?

So sánh chuỗi bằng cách sử dụng == trong python hàm == so sánh các giá trị của hai chuỗi và trả về nếu chúng bằng hoặc không.Nếu các chuỗi bằng nhau, nó sẽ trả về đúng, nếu không nó sẽ trả về sai.The == function compares the values of two strings and returns if they are equal or not. If the strings are equal, it returns True, otherwise it returns False.

Bạn có thể so sánh hai chuỗi bằng toán tử == không?

Toán tử ==, được gọi là toán tử bình đẳng, được sử dụng để so sánh hai chuỗi trong Java.

Làm thế nào để == hoạt động trong Python?

Đặt đơn giản: == Xác định xem các giá trị của hai đối tượng có bằng nhau hay không, trong khi xác định nếu chúng là cùng một đối tượng.*id () là một hàm tích hợp trong Python.Nó chấp nhận một tham số duy nhất và được sử dụng để trả về danh tính của một đối tượng.determines if the values of two objects are equal, while is determines if they are the exact same object. *id() is a built-in function in Python. It accepts a single parameter and is used to return the identity of an object.