0

Basically I'm trying to create a python printmessage function and my brain is kind of breaking up right now, I can usually solve this in 2 seconds but something just isn't working. For the current code I have the following and am trying to fix it up:

def printmsg(type, message):
    lettercount = 0
    print(f"{colorama.Fore.RED}[DEBUG]{colorama.Fore.RESET} {len(type)}")
    varspaces = ""
    for i in range(10-len(type)):
        varspaces = varspaces + " "
    print(f"{colorama.Fore.RED}[{colorama.Fore.RESET} {type} {colorama.Fore.RED}] {varspaces} | {message}")

I just really want to know how to remove the for i in range(10-len(type)) and make it something like varspaces = 10-len(type) * " ".

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 5
    `varspaces = (10-len(type)) * " "` – Macattack Feb 05 '21 at 19:01
  • 3
    Additionally, change the variable name `type` to something else. `type` is a keyword. – arshovon Feb 05 '21 at 19:02
  • 3
    @arsho: `type` is not a keyword: `keyword.iskeyword('type') → False` — but using it as the name of a variable, argument, function, class, etc is still not a good idea because it's the name of a **built-in**. If it was a keyword, you couldn't do that anyway. – martineau Feb 05 '21 at 19:08
  • 1
    @martineau, you are absolutely right. I did not know about the `keyword` module! Thank you for the enlightenment. The list of keywords of Python are listed here: https://docs.python.org/3/reference/lexical_analysis.html#keywords – arshovon Feb 05 '21 at 19:14
  • 1
    @arsho: Yes, they're all listed in the documentation—thanks for posting the link—but sometimes using the `keyword` module is more handy. I wish there was something similar for the built-ins (besides the [documentation](https://docs.python.org/3/library/functions.html)). – martineau Feb 05 '21 at 19:21

0 Answers0