Remove part of string within a loop in python

Keep in mind this in within a loop.

How can I remove everything from "?" and so on?

So that "something_else_1" gets deleted

Url_before = ""

Url_wanted = ""

In practice it looks kinda like this:

find_href = driver.find_elements(By.CSS_SELECTOR, 'img.MosaicAsset-module__thumb___yvFP5')

with open("URLS/text_urls.txt", "a+") as textFile:
            for my_href in find_href:

>Solution :

Provided there’s only one instance of "?" in the string and you want to remove everything after it, you could find the index of this character with

i = Url_before.index("?")

and then remove everything after it:

Url_wanted = Url_before[:i+1]

Leave a Reply