random.choice(symbols) is the choice of one random symbol from symbols . If you execute passwd = random.choice(symbols) in a loop, each time the previous character will be overwritten. Line breaks have nothing to do with it.
If you need a string of 12 characters, you need to add each character to passwd , and not to overwrite passwd :
@app.route('/') def passwd_generation(): symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVYXWZ~$&_+-=/\\" passwd = '' # начальное значение - пустая строка for i in range(12): passwd += random.choice(symbols) # добавляем символ к строке return passwd
Regarding return(random.choice(symbols), end='') - this construction does not make sense. You probably borrowed end='' from a print(random.choice(symbols), end='') form print(random.choice(symbols), end='') , like this:
end is a parameter to the print function, it will not work with returnreturn is not a function, it is an operator, no brackets are required when using it
Instead of a cycle with an accumulated value, you can use the generator:
@app.route('/') def passwd_generation(): symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVYXWZ~$&_+-=/\\" passwd = ''.join(random.choice(symbols) for _ in range(12)) return passwd
You can read about such constructions, for example, here:
Basics of Python - briefly. Part 4. List generators
By the way, @app.route('/') is a function decorator , it refers to the function below, and not the code above, so it’s better to write it along with the function (as I have in the examples), and vice versa add empty lines above it (unless of course there is no other decorator).