944,124 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 319
  • Python RSS
Oct 29th, 2009
0

noob question about strings

Expand Post »
I want to create a string like this:
(just theoretically:-))
Python Syntax (Toggle Plain Text)
  1. import random
  2.  
  3. list1 = ["1", "2", "3"]
  4. string1 = random.choice(list1)
  5. 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

Python Syntax (Toggle Plain Text)
  1. """Random Number is
  2.  
  3. 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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mishu5770l is offline Offline
2 posts
since Oct 2009
Oct 29th, 2009
1
Re: noob question about strings
try
python Syntax (Toggle Plain Text)
  1. string2 = "Random Number is\n{value:^16}".format(value = string1)
Also, you can read this
Last edited by Gribouillis; Oct 29th, 2009 at 12:59 am.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Oct 29th, 2009
0
Re: noob question about strings
thank you!

so the \n has to be in quotes.
I suppose I could eve do it like this:
Python Syntax (Toggle Plain Text)
  1. string2 = "Random Number is" + "\n\t" + string1
but that other formatting stuff is really going to come in handy!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mishu5770l is offline Offline
2 posts
since Oct 2009
Oct 29th, 2009
0
Re: noob question about strings
The other option is ...
python Syntax (Toggle Plain Text)
  1. import random
  2.  
  3. list1 = ["1", "2", "3"]
  4. string1 = random.choice(list1)
  5. string2 = """\
  6. Random Number is
  7.  
  8. %s""" % string1
  9.  
  10. print(string2)
... this makes the output more visual in your code.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Getting the connected user's ip in python
Next Thread in Python Forum Timeline: pygame - filling in a block on a grid





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC