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
  How to remove char from string?? ridgerunnersjw 2 153 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  SyntaxError: unexpected character after line continuation character siteshkumar 2 346 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Remove from end of string up to and including some character lbtdne 2 470 May-17-2020, 09:24 AM
Last Post: menator01
  Remove escape characters / Unicode characters from string DreamingInsanity 5 939 May-15-2020, 01:37 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 583 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Highlight and remove specific string of text itsalmade 5 720 Dec-11-2019, 11:58 PM
Last Post: micseydel
  How to get the index of a character from a string chihaya 1 498 Dec-03-2019, 12:54 PM
Last Post: buran
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 2,276 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  python gives wrong string length and wrong character thienson30 2 664 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  with input remove a string from the list konsular 3 589 Oct-12-2019, 09:25 AM
Last Post: konsular

Forum Jump:

User Panel Messages

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