Raman
Raman

Reputation: 13

String operation in Python. Parse URL

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

Result:

127.0.0.1:8000/test/en/

or

127.0.0.1:8000/test/ru/

How to check if the end of the string is /en/ or /ru/

Upvotes: 1

Views: 105

Answers (2)

Frodon
Frodon

Reputation: 3775

You can test the four final characters:

full_url[-4:]

or use the ends_with string method:

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

Upvotes: 0

Frerich Raabe
Frerich Raabe

Reputation: 94469

Use endswith:

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

If you find that there are more extensions you have to cover, you may consider using any:

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

Upvotes: 6

Related Questions