# Write a Python program to print the following string in a specific format (see the output).
# Sample String : "Twinkle, twinkle, little star,
# How I wonder what you are! Up above the world so high,
# Like a diamond in the sky.
# Twinkle, twinkle, little star, How I wonder what you are"
#
# Output :
# Twinkle, twinkle, little star,
#  How I wonder what you are!
#     Up above the world so high,
#     Like a diamond in the sky.
# Twinkle, twinkle, little star,
#  How I wonder what you are

sampletext = """Twinkle, twinkle, little star,
How I wonder what you are! Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star, How I wonder what you are"""

newsample = """Twinkle, twinkle, little star,
\n \t How I wonder what you are! 
\n \t \t Up above the world so high,
\n \t \t Like a diamond in the sky.
\n Twinkle, twinkle, little star,
\n \t How I wonder what you are"""

print(newsample)

So lets start with the basics:

  1. # (aka hashtag) is used to comment a line.
  2. Single line strings are stored in either single quotes (‘…’) or double quotes (“…”).
  3. Multiple line strings are stored in triple quotes (“””…”””) or 3 single quotes.
  4. \n – Escape sequence used to add a new line.
  5. \t – Escape sequence used to add tab (space) before the text. Tab generally ends up adding 4 spaces before start of the string.

So the trick was very simple here. Using escape sequence, you can simply display your text in the manner asked.

Note:

  1. Using hash tag for multiple lines is not possible like what we did for multi line string. Hash tag needs to be added on each line.
  2. I’m using Pycharm by JetBrains. It is very similar to Visual Studio or any other heavy text editors. I googled a lot and ended up for this since I had previous experience with VS and I found it easy to have my words print before me even before I complete them. #lazyassproblems
  3. I think its a beautiful app though. 🙂
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s