User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,858 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,593 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:

Starting Python

Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: Starting Python

  #23  
Jul 22nd, 2005
In a recent thread we looked at ways to create a multiline string. Here are a few code samples showing you how to do this, some are preference, some are cosmetic ...
  1. # a look at multiline strings
  2.  
  3. # using line continuation (\) to form one long/multiline string
  4. # complex and tough to read
  5. # (no space right after \ or an empty line following \)
  6. str1 = "The alien gasps and says, 'Oh, this is it. I will die! \n\
  7. Tell my 2.4 million larvae that I loved them... \n\
  8. Good-bye, cruel universe.' "
  9.  
  10.  
  11. # using line continuation (\) to combine three strings to one string
  12. # ignores indentations between strings, easy on the eye
  13. str2 = "The alien gasps and says, 'Oh, this is it. I will die! \n"\
  14. "Tell my 2.4 million larvae that I loved them... \n"\
  15. "Good-bye, cruel universe.'"
  16.  
  17.  
  18. # using just triple quotes, still tough to read?
  19. # note: newline characters are taken care of
  20. str3 = """The alien gasps, 'Oh, this is it. I will die!
  21. Tell my 2.4 million larvae that I loved them...
  22. Good-bye, cruel universe.' """
  23.  
  24.  
  25. # using line continuation (\) and triple quotes
  26. # might be easier to read
  27. str4 = \
  28. """The alien gasps and says, 'Oh, this is it. I will die!
  29. Tell my 2.4 million larvae that I loved them...
  30. Good-bye, cruel universe.' """
  31.  
  32.  
  33. # note: indentations become part of the string
  34. str5 = """The alien gasps, 'Oh, this is it. I will die!
  35. Tell my 2.4 million larvae that I loved them...
  36. Good-bye, cruel universe.' """
  37.  
  38.  
  39. # gee, one more way to do a multiline string with the + concatinator ...
  40. str6 = "The alien gasps and says, 'Oh, this is it. I will die! \n"
  41. str6 = str6 + "Tell my 2.4 million larvae that I loved them... \n"
  42. str6 = str6 + "Good-bye, cruel universe.'"
  43.  
  44.  
  45. # another variation using +
  46. str7 = "The alien gasps and says, 'Oh, this is it. I will die! \n"
  47. str8 = "Tell my 2.4 million larvae that I loved them... \n"
  48. str9 = "Good-bye, cruel universe.'"
  49. str10 = str7 + str8 + str9
  50.  
  51.  
  52. print '-'*60 # pretty up with 60 dashes
  53. print str1
  54. print '-'*60
  55. print str2
  56. print '-'*60
  57. print str3
  58. print '-'*60
  59. print str4
  60. print '-'*60
  61. print str5
  62. print '-'*60
  63. print str6
  64. print '-'*60
  65. print str10
BTW, the php code field seems to eat up some of the trailing \ characters, so I switched to the regular code=python field.
Last edited by vegaseat : Mar 1st, 2007 at 2:44 pm. Reason: [code=python] tag
May 'the Google' be with you!
Reply With Quote  
All times are GMT -4. The time now is 6:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC