pythonvariables

How do I change output of variable in python?


My current code is

#imports
import requests
#setup
response = requests.get("https://randomfox.ca/floof/")
fox = response.json()
#code
print(fox['image'])

Whenever I run it, it always prints something like https://randomfox.ca/images/99.jpg. Is there any way to change this and make it 99.jpg instead?


Solution

  • import requests
    import os
    
    # Setup
    response = requests.get("https://randomfox.ca/floof/")
    fox = response.json()
    
    # Extract the filename
    image_url = fox['image']
    filename = os.path.basename(image_url)
    
    # Code
    print(filename)