Noooo
Noooo

Reputation: 68

What's the more pythonic way of formatting a huge set of strings while maintaining good readability and white spaces?

I'm working on a Mad Lib project. I have a whole story that I would like to print but I get unwanted indentions whenever I use triple quotes to define my strings. When I try to get over this by removing the tabs and spaces that I use to keep it neat and readable, it just looks unpleasant. I ended up going with the code below but I was thinking that maybe there's a better way to do this. Will there be a more pythonic way of doing formatting this?

The code that I ended up with:

name, place1, place2, adj1, adj2, adj3, pNoun1, pNoun2, pNoun3, pNoun4,
aVerb1, aVerb2, aVerb3, noun = None

print ('Last summer, my mom and dad took me and %s on a trip to %s. ', % name, place1,
       'The weather there is very %s! Northern %s has many %s, and ', % adj1, place1, pNoun1
       'they make %s %s there. Many people also go to %s to %s or see ', % adj2, pNoun2, place2, aVerb1
       'the %s. The people that live there love to eat %s and are very ', % pNoun3, pNoun4
       'proud of their big %s. They also liketo %s in the sun and in the ', % noun, aVerb2
       '%s! It was a really %s trip!' % aVerb3, adj3)

At first I was doing it like this but it ended up with unwanted new lines and indentions:

print('''Last summer, my mom and dad took me and %s on a trip to %s.
     The weather there is very %s! Northern %s has many %s, and they
     make %s %s there. Many people also go to %s to %s or see the %s.
     The people that live there love to eat %s and are very proud of
     their big %s. They also liketo %s in the sun and in the %s! It 
     was a really %s trip!''' %  (name, place1, adj1, place1, 
     pNoun1,adj2, pNoun2, place2, aVerb1, pNoun3, pNoun4, noun, aVerb2,
     aVerb3, adj3)) 

Upvotes: 0

Views: 86

Answers (4)

Menglong Li
Menglong Li

Reputation: 2255

@CtheSky 's answer is OK, but I prefer using Template from string, it has a method safe_substitute which can handle the case if some keys are missing, no exception will be raised.

infos = {
    'name': 'XXX',
    'noun': 'noun',
    'adj1': 'adj1',
    'adj2': 'adj2',
    'adj3': 'adj3',
    'aVerb1': 'aVerb1',
    'aVerb2': 'aVerb2',
    'aVerb3': 'aVerb3',
    'place1': 'place1',
    'place2': 'place2',
    'pNoun1': 'pNoun1',
    'pNoun2': 'pNoun2',
    'pNoun3': 'pNoun3',
    'pNoun4': 'pNoun4',
}

from string import Template

tpl = Template('''Last summer, my mom and dad took me and $name on a trip to %s.
     The weather there is very $adj1! Northern $place1 has many $pNoun1, and they
     make $adj2 $pNoun2 there. Many people also go to $place2 to $aVerb1 or see the $pNoun3.
     The people that live there love to eat $pNoun4 and are very proud of
     their big $noun. They also like to $aVerb2 in the sun and in the $aVerb3! It 
     was a really $adj3 trip!''')

print(tpl.safe_substitute(**infos))

Upvotes: 1

VPfB
VPfB

Reputation: 17332

There are at least two common ways how to avoid extra indentation in triple quoted text.

Write the text starting from column 0:

def func():
    txt = """\
line1
line2
line3
"""
    pass

Which is arguably ugly.

Or use dedent. An example is included in the link.

Upvotes: 0

CtheSky
CtheSky

Reputation: 2624

You can use format with Accessing arguments by name, see the documentation.

Try:

infos = {
    'name': 'name',
    'noun': 'noun',
    'adj1': 'adj1',
    'adj2': 'adj2',
    'adj3': 'adj3',
    'aVerb1': 'aVerb1',
    'aVerb2': 'aVerb2',
    'aVerb3': 'aVerb3',
    'place1': 'place1',
    'place2': 'place2',
    'pNoun1': 'pNoun1',
    'pNoun2': 'pNoun2',
    'pNoun3': 'pNoun3',
    'pNoun4': 'pNoun4',
}

print('''Last summer, my mom and dad took me and {name} on a trip to %s.
     The weather there is very {adj1}! Northern {place1} has many {pNoun1}, and they
     make {adj2} {pNoun2} there. Many people also go to {place2} to {aVerb1} or see the {pNoun3}.
     The people that live there love to eat {pNoun4} and are very proud of
     their big {noun}. They also liketo {aVerb2} in the sun and in the {aVerb3}! It 
     was a really {adj3} trip!'''.format(**infos))

And you can reuse the name argument in format to be more flexible:

print('{pNoun1} {aVerb1} {pNoun1}'.format(**infos))
=> pNoun1 aVerb1 pNoun1

Upvotes: 3

Kyle Higginson
Kyle Higginson

Reputation: 952

Try the new style for formatting using the format() method. Which works like this:

print ("Last summer, my mom and dad took me and {} on a trip to 
{}.".format(name, place1))

Upvotes: 1

Related Questions