Python Forum
remove string character from url
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remove string character from url
#11
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove gilberishs from a "string" kucingkembar 2 202 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
Smile please help me remove error for string.strip() jamie_01 3 1,151 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,306 Sep-27-2022, 01:38 PM
Last Post: buran
  Remove a space between a string and variable in print sie 5 1,706 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  How do I remove spurious "." from a string? Zuhan 7 1,962 Apr-12-2022, 02:06 PM
Last Post: Pedroski55
  Regex: a string does not starts and ends with the same character Melcu54 5 2,367 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  [solved] unexpected character after line continuation character paul18fr 4 3,294 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  How to remove char from string?? ridgerunnersjw 2 2,484 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,107 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Remove from end of string up to and including some character lbtdne 2 2,285 May-17-2020, 09:24 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020