Python Forum

Full Version: remove string character from url
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
That's different:
url1 = 'https://www.facebook.com/xxxxxx/test1q223/'
url2 = 'https://www.facebook.com/xxxxxx/?test1q223'
url3 = 'https://www.facebook.com/xxxxxx/test1q223'
url4 = 'https://www.facebook.com/xxxxxx/test1q223'

# def change_url(url):
#     urlx = url.split('/')
#     if url[-1] == '/':
#         return url[:-1]
#     if urlx[-1].startswith('?'):
#         urlx[-1] = urlx[-1][1:]
#         return '/'.join(urlx)
#     return url

def change_url(url):
    idx = url.index('xxxxxx/')
    return url[:idx+6]

print(f'url1: {change_url(url1)}')
print(f'url2: {change_url(url2)}')
print(f'url3: {change_url(url3)}')
print(f'url4: {change_url(url4)}')
output:
Output:
url1: https://www.facebook.com/xxxxxx/ url2: https://www.facebook.com/xxxxxx/ url3: https://www.facebook.com/xxxxxx/ url4: https://www.facebook.com/xxxxxx/
you can discard commented code.
Please spend some time and learn about slicing, see slicing section here: https://www.python-course.eu/python3_seq..._types.php

** Note ** Edited Mar25 11:54 EDT To make mod safer
Pages: 1 2