Python中的字符串操作。解析URL

时间:2013-07-19 10:42:45

标签: python

print full_url = request.get_host()+request.get_full_path()

结果:

  

127.0.0.1:8000/test/en /

  

127.0.0.1:8000/test/ru /

如何检查字符串的结尾是/en/还是/ru/

2 个答案:

答案 0 :(得分:6)

使用endswith

full_url.endswith('/en/') or full_url.endswith('/ru/')

如果您发现必须覆盖更多扩展程序,可以考虑使用any

any(s.endswith(ext) for ext in ['/ru/', '/en/'])

答案 1 :(得分:0)

您可以测试最后四个字符:

full_url[-4:]

或使用ends_with字符串方法:

if full_url.ends_with("/en/"):
  print 'en'
elif full_url.ends_with("/ru/"):
  print 'ru'