I want to create a string like this:
(just theoretically:-))

import random

list1 = ["1", "2", "3"]
string1 = random.choice(list1)
string2 = "Random Number is" + string1

but I want string2 to show up formatted like this:


Random Number is
2


But, for the life of me I cant figure out how to create a new line and a tab, without writing string2 like this

"""Random Number is

                2"""

I spent 30 minutes in the documentation and on google, and all i found was "\t" and "\n", but I cant even figure out how to make them work! Any help would be greatly appreciated, Thanks!

try

string2 = "Random Number is\n{value:^16}".format(value = string1)

Also, you can read this :)

Edited 7 Years Ago by Gribouillis: n/a

thank you!

so the \n has to be in quotes.
I suppose I could eve do it like this:

string2 = "Random Number is" + "\n\t" + string1

but that other formatting stuff is really going to come in handy!

The other option is ...

import random

list1 = ["1", "2", "3"]
string1 = random.choice(list1)
string2 = """\
Random Number is

       %s""" % string1

print(string2)

... this makes the output more visual in your code.

This question has already been answered. Start a new discussion instead.