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!

Recommended Answers

All 3 Replies

try

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

Also, you can read this :)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.