You can encode a URL or part of a URL which contains invalid URL characters. This means that the invalid URL characters will be replaced by their valid HTML character combinations. An example for a URL particle containing a * (asterisk):
{{ "seg*ment"|url_encode }}
Result
seg%2Ament
Another example is having one or more spaces in the URL:
{{ "one space"|url_encode }}
Result
one%20space
In the next example, a key-value pair is converted into valid URL parameters:
{{ {'key': 'value', 'foo': 'bar'}|url_encode }}
Result
key=value&foo=bar