Hướng dẫn python compare two strings character by character according to their values - python so sánh hai chuỗi ký tự theo ký tự theo giá trị của chúng

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:

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')

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.

Hãy cho chúng tôi xem cách so sánh các chuỗi trong Python. & NBSP;

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

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.

Example:

"Geek geek

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

Hãy cho chúng tôi hiểu điều này với một ví dụ:

Python3

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.
9no___Trans___Pre___16 no___Trans___Pre___15

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

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

True
False
False
False
True
True
3
True
False
False
False
True
True
4
False
True
A unicode is 65 ,a unicode is 97
0
False
True
A unicode is 65 ,a unicode is 97
1
False
True
A unicode is 65 ,a unicode is 97
2
True
False
False
False
True
True
4
False
True
A unicode is 65 ,a unicode is 97
4
False
True
A unicode is 65 ,a unicode is 97
5

True
False
False
False
True
True
3
True
False
False
False
True
True
4
False
True
A unicode is 65 ,a unicode is 97
8
False
True
A unicode is 65 ,a unicode is 97
1
False
True
A unicode is 65 ,a unicode is 97
2
True
False
False
False
True
True
4
False
True
A unicode is 65 ,a unicode is 97
4
print('Apple' < 'ApplePie')
3

True
False
False
False
True
True
3
True
False
False
False
True
True
4
print('Apple' < 'ApplePie')
6
False
True
A unicode is 65 ,a unicode is 97
1
False
True
A unicode is 65 ,a unicode is 97
2
True
False
False
False
True
True
4
False
True
A unicode is 65 ,a unicode is 97
4
print('apple' < 'apple')
print('apple' > 'apple')
1

True
False
False
False
True
True
3no___Trans___Pre___73no___Trans___Pre___74 no___trans___pre___75

True
False
False
False
True
True
3no___Trans___Pre___73no___Trans___Pre___74 no___trans___pre___79

True
False
False
False
True
True
3no___Trans___Pre___73no___Trans___Pre___74 no___trans___pre___83

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.
9no___Trans___Pre___85no___Trans___Pre___16 no___trans___pre___87

False
False
8no___Trans___Pre___16 no___Trans___Pre___90

True
False
False
False
True
True
3
True
False
False
False
True
True
4
True
True
False
False
3
False
True
A unicode is 65 ,a unicode is 97
1
False
True
A unicode is 65 ,a unicode is 97
2
True
False
False
False
True
True
4
False
True
A unicode is 65 ,a unicode is 97
4
False
True
A unicode is 65 ,a unicode is 97
5

True
False
False
False
True
True
3
True
False
False
False
True
True
4
True
False
False
False
True
True
01
False
True
A unicode is 65 ,a unicode is 97
1
False
True
A unicode is 65 ,a unicode is 97
2
True
False
False
False
True
True
4
False
True
A unicode is 65 ,a unicode is 97
4
True
False
False
False
True
True
06

True
False
False
False
True
True
3no___Trans___Pre___73no___Trans___Pre___74 no___trans___pre___110

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.

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___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

Output:

True
False
False
False
True
True
1

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

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?

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?

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?

Cách so sánh các chuỗi bằng toán tử ==.Chúng tôi có một giá trị của việc trả về thực sự vì cả hai chuỗi trên đều bằng nhau.Trong mã trên, chúng tôi đã tạo hai chuỗi và lưu trữ chúng theo các biến.Sau đó chúng tôi so sánh các giá trị của họ.