当前位置:  首页>> 技术小册>> Python合辑4-130个字符串操作示例

Python比较两个字符串保留一端的差异

  1. def after(s1, s2):
  2. index = s1.find(s2)
  3. if index != -1 and index + len(s2) < len(s1):
  4. return s1[index + len(s2):]
  5. else:
  6. return None
  7. s1 = "canada"
  8. s2 = "can"
  9. print(after(s1, s2))

Output:

  1. ada