เมื่อต้องใช้สองสตริงและแสดงสตริงที่ใหญ่กว่าโดยไม่ต้องใช้ฟังก์ชันในตัว สามารถใช้การวนซ้ำอย่างง่ายและตัวดำเนินการ '=='
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ตัวอย่าง
string_1 = "Malala" string_2 = "Male" count_1 = 0 count_2 = 0 print("The first string is :") print(string_1) print("The second string is :") print(string_2) for i in string_1: count_1 = count_1+1 for j in string_2: count_2 = count_2+1 if(count_1<count_2): print("The larger string is:") print(string_2) elif(count_1==count_2): print("Both the strings are equal.") else: print("The larger string is:") print(string_1)
ผลลัพธ์
The first string is : Malala The second string is : Male The larger string is: Malala
คำอธิบาย
-
มีการกำหนดสองสตริงและแสดงบนคอนโซล
-
ตัวนับสองตัวเริ่มต้นเป็น 0
-
สตริงจะถูกวนซ้ำและได้รับความยาวของมัน
-
นี้จะเพิ่มขึ้นและเก็บไว้ในเคาน์เตอร์
-
ขึ้นอยู่กับค่าการนับ สตริงที่ใหญ่กว่าของทั้งสองสตริงจะแสดงบนคอนโซล