Search code examples
pythonlistpython-webbrowser

How do you put lists in webbrowser links?


I'm trying to create a program that opens a random Bitly link in your browser.

import random
import webbrowser

#set the length of the url
length = random.randint(1,7)

#list of all possible characters in the key
characters = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h',
 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P',
  'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y',
   'Y', 'z', 'Z',]

#create the string of text on the end of the link
key = []
for x in range (1, length):
    key.append(random.choice(characters))

webbrowser.open_new_tab('https://bit.ly/', *key, sep='')

When I do this it gives me

TypeError: open_new_tab() got an unexpected keyword argument 'sep'

But if I can't put "sep" into that argument, how else am I suposed to make the link open correctly?


Solution

  • You can use

    key_str = "".join(key)
    

    to join all of the characters in key into a single string. The URL could be constructed with

    url = f"https://bit.ly/{key_str}"
    

    By the way, instead of defining a list of upper- and lower-case letters, you can use string.ascii_letters.