noob question about strings

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 2
Reputation: mishu5770l is an unknown quantity at this point 
Solved Threads: 0
mishu5770l mishu5770l is offline Offline
Newbie Poster

noob question about strings

 
0
  #1
Oct 29th, 2009
I want to create a string like this:
(just theoretically:-))
  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

  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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 930
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
1
  #2
Oct 29th, 2009
try
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: mishu5770l is an unknown quantity at this point 
Solved Threads: 0
mishu5770l mishu5770l is offline Offline
Newbie Poster
 
0
  #3
Oct 29th, 2009
thank you!

so the \n has to be in quotes.
I suppose I could eve do it like this:
  1. string2 = "Random Number is" + "\n\t" + string1
but that other formatting stuff is really going to come in handy!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,025
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #4
34 Days Ago
The other option is ...
  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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC