如何比较两个字母相同数字不同的字符串?

2024-04-25 23:14:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个字符串“s1”和“s2”

有没有办法不改变这两个值就得到“s1”大于“s2”的结果?你知道吗


Tags: 字符串s2办法s1
2条回答

只需使用><。。你知道吗

>>> 's1' > 's2'
False
>>> 's1' < 's2'
True

有办法。见下表:

s1 = "abcdefg"
s2 = "abcdefghijklmnop"

if len(s1) > len(s2):
    print("String 1 is longer than String 2")
elif len(s1) < len(s2):
    print("String 2 is longer than String 1")
else:
    print("String 1 and 2 are the same length.")

这将比较字符串s1和s2的长度。如果这不是你要找的,那我不确定。你知道吗

相关问题 更多 >