Hướng dẫn character comparison in python - so sánh nhân vật 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.

Show

Nội dung chính ShowShow

  • So sánh chuỗi Python
  • Làm thế nào để bạn so sánh hai chuỗi char bằng ký tự trong Python?
  • Làm thế nào để bạn so sánh hai chuỗi trong nếu điều kiện python?
  • Làm thế nào để bạn so sánh hai chuỗi hoặc giá trị là như nhau?
  • 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 Python

Làm thế nào để bạn so sánh hai chuỗi char bằng ký tự trong Python?

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

Làm thế nào để bạn so sánh hai chuỗi trong nếu điều kiện python?

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.

Làm thế nào để bạn so sánh hai chuỗi hoặc giá trị là như nhau?

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

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

print('Apple' < 'ApplePie')

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.

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.

Output:

False
False

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?

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?

Output:

True
False
False
False
True
True
2 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')
Using Relational Operators

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.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:

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.

Hãy cho chúng tôi xem cách so sánh các chuỗi trong Python. & NBSP;
“Geek” < “geek” will return True and
“Geek” > “geek” will return False

Python3

Phương pháp 1: Sử dụng các toán tử quan hệUsing Relational Operators

Output:

True
False
False
False
True
True
4

Các toán tử quan hệ so sánh các giá trị unicode của các ký tự của các chuỗi từ chỉ số zeroth cho đến khi kết thúc chuỗi. Sau đó, nó trả về một giá trị boolean theo nhà điều hành được sử dụ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.Using is and is not

"Geek geek== 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.

Trong trường hợp của Geek Geek và và Geek Geek, vì unicode của g là \ u0047 và của g là \ u0067“Geek” < “geek” will return True and“Geek” > “geek” will return False

Python3

True
False
False
False
True
True
3no___trans___pre___14no___trans___pre___15 no___trans___pre___16no___trans___pre___16 no___trans___pre___15no___trans___
True
False
False
False
True
True
3no___Trans___Pre___14no___trans___pre___15 no___trans___pre___23no___trans___pre___24no___trans___pre___19
True
False
False
False
True
True
3no___Trans___Pre___14no___Trans___Pre___15 no___trans___pre___29no___trans___pre___24no___trans___pre___19
True
False
False
False
True
True
3no___trans___pre___14no___trans___pre___15 no___trans___pre___35no___trans___pre___16 no___trans___pre___15no___trans___

Output:

True
False
False
False
True
True
0

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ố.

Python3

NO___Trans___Pre___111

True
False
False
False
True
True
12
True
False
False
False
True
True
13no___Trans___Pre___114no___Trans___Pre___16 no___trans___pre___116
True
False
False
False
True
True
13no___Trans___Pre___118no___Trans___Pre___16 no___trans___pre___116

NO___Trans___Pre___113no___trans___pre___122 no___trans___pre___123no___trans___pre___124 no___trans___pre___125no___trans___pre___14no_

No

True
False
False
False
True
True
39no___Trans___Pre___114no___Trans___Pre___85no___trans___pre___16 no___trans___pre___143

NO___Trans___Pre___113no___trans___pre___122 no___trans___pre___123no___trans___pre___124 no___trans___pre___125no___trans___pre___14no_

No

True
False
False
False
True
True
39no___Trans___Pre___114no___Trans___Pre___85no___trans___pre___16 no___trans___pre___143

Output:

True
False
False
False
True
True
39no___Trans___Pre___118no___Trans___Pre___85no___trans___pre___16 no___trans___pre___143
True
False
False
False
True
True
13no___Trans___Pre___168 no___trans___pre___114no___trans___pre___16no___trans___pre___16 no___trans___pre___172
True
False
False
False
True
True
3
True
False
False
False
True
True
74
True
False
False
False
True
True
75
False
True
A unicode is 65 ,a unicode is 97
1
True
False
False
False
True
True
77
True
False
False
False
True
True
78
True
False
False
False
True
True
3
True
False
False
False
True
True
74
True
False
False
False
True
True
77
False
True
A unicode is 65 ,a unicode is 97
1
True
False
False
False
True
True
83
True
False
False
False
True
True
78
True
False
False
False
True
True
3
True
False
False
False
True
True
74
True
False
False
False
True
True
87
False
True
A unicode is 65 ,a unicode is 97
1
True
False
False
False
True
True
89
True
False
False
False
True
True
78

Làm thế nào để bạn so sánh hai chuỗi char bằng ký tự trong Python?

True
False
False
False
True
True
1Làm thế nào để bạn so sánh hai chuỗi char bằng ký tự trong Python?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.

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.

Làm thế nào để bạn so sánh hai chuỗi trong nếu điều kiện python? for comparing two strings If you simply require comparing the values of two variables then you may use the '==' operator. If strings are same, it evaluates as True, otherwise False.

Sử dụng toán tử == (bằng) để so sánh hai chuỗi nếu bạn chỉ yêu cầu so sánh các giá trị của hai biến thì bạn có thể sử dụng toán tử '=='.Nếu chuỗi giống nhau, nó sẽ đánh giá là đúng, nếu không là sai. for comparing two strings If you simply require comparing the values of two variables then you may use the '==' operator. If strings are same, it evaluates as True, otherwise False.

Làm thế nào để bạn so sánh hai chuỗi hoặc giá trị là như nhau? equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string. This method returns true if the argument is not null and the contents of both the Strings are same ignoring case, else false.

Sử dụng phương thức String.equalSignorecase () so sánh hai chuỗi không phân biệt trường hợp (dưới hoặc trên) của chuỗi.Phương thức này trả về đúng nếu đối số không phải là null và nội dung của cả hai chuỗi là cùng một trường hợp bỏ qua, khác là sai.equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string. This method returns true if the argument is not null and the contents of both the Strings are same ignoring case, else false.

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