Quote:Why does my first url1 didn't cut off the last string" /test1q223 "?
Break it down step by step (uses f-string which requires python 3.6 or newer)
>>> url1 = 'https://www.facebook.com/xxxxxx/test1q223/'
>>> url2 = 'https://www.facebook.com/xxxxxx/?test1q223'
>>> url3 = 'https://www.facebook.com/xxxxxx/test1q223'
>>> def change_url(url):
... urlx = url.split('/')
... print(f'url: {url}, urlx: {urlx}')
... if url[-1] == '/':
... print(f'returning url[-1]: {url[-1]}')
... return url[:-1]
... if urlx[-1].startswith('?'):
... print(f'urlx[-1][1:]: {urlx[-1][1:]}')
... print(f"returning '/'.join(urlx): {'/'.join(urlx)}")
... return '/'.join(urlx)
... # No change needed
... return url
...
>>> print(f'url1: {change_url(url1)}')
url: https://www.facebook.com/xxxxxx/test1q223/, urlx: ['https:', '', 'www.facebook.com', 'xxxxxx', 'test1q223', '']
returning url[-1]: /
url1: https://www.facebook.com/xxxxxx/test1q223
>>> # ------------------------------------------
...
>>> print(f'url2: {change_url(url2)}')
url: https://www.facebook.com/xxxxxx/?test1q223, urlx: ['https:', '', 'www.facebook.com', 'xxxxxx', '?test1q223']
urlx[-1][1:]: test1q223
returning '/'.join(urlx): https://www.facebook.com/xxxxxx/?test1q223
url2: https://www.facebook.com/xxxxxx/?test1q223
>>> # ------------------------------------------
...
>>> print(f'url3: {change_url(url3)}')
url: https://www.facebook.com/xxxxxx/test1q223, urlx: ['https:', '', 'www.facebook.com', 'xxxxxx', 'test1q223']
url3: https://www.facebook.com/xxxxxx/test1q223
>>>