Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何比较两个具有相同字母和不同数字的字符串?_Python - Fatal编程技术网

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

Python 如何比较两个具有相同字母和不同数字的字符串?,python,Python,我有两个字符串“s1”和“s2” 有没有什么方法可以在不改变两个值的情况下得到“s1”大于“s2”的结果 有一种方法。见下文: 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") el

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


有没有什么方法可以在不改变两个值的情况下得到“s1”大于“s2”的结果

有一种方法。见下文:

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=“abcdefg”
s2=“abcdefghijklmnop”
如果len(s1)>len(s2):
打印(“字符串1比字符串2长”)
elif len(s1)

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

只需使用
>>'s1'>'s2'
假的
>>>“s1”<“s2”
真的

>>不是('s1'>'s2')
?我不太明白你想达到什么目的。你能详细说明你希望得到什么样的投入吗?输入总是一个字母和一个数字,还是可以是其他的?您想对这些字符串施加什么顺序?
'a1'
应该大于还是小于
'b1'
?那么
'a2'
'b1'
呢?您想对您定义的顺序做什么?对字符串进行排序?@warwaruk我犯了一个可怕的错误,我认为字符串与它的值不可比较,但我测试了自己,我错了。很抱歉这篇文章引起了大家的注意
>>> 's1' > 's2'
False
>>> 's1' < 's2'
True