dylnmc
dylnmc

Reputation: 4010

Python format inside brackets

Is it possible to format inside of the brackets? What I mean is this:

print "Formatting: { 0 : {1} s }".format("""\
long piece of text that I need to format, but I can't explicitly say\
how long it is going to be because it is coming from another function\
that gives me the length of how many characters I can print. So, it would be\
really helpful if someone could help me with this""" , charMax())

import random

def charMax():
    return random.randint(10, 80)

Can anyone help me establish how to emulate the sudo code above?

Upvotes: 0

Views: 3434

Answers (3)

A.J. Uppal
A.J. Uppal

Reputation: 19264

You most likely need a function if you want a string of desired length, formats aren't going to do much for you:

def charMax():
    return random.randint(10, 80)

def stringLength(originalStr, length):
    return originalStr[:length]

And then you can do the formatting:

print "Formatting: {0}".format(stringLength("long piece of text that I need to format, but I can't explicitly say how long it is going to be because it is coming from another function that gives me the length of how many characters I can print. So, it would be really helpful if someone could help me with this", charMax()))

>>> print "Formatting: {0}".format(stringLength("long piece of text that I need to format, but I can't explicitly say how long it is going to be because it is coming from another function that gives me the length of how many characters I can print. So, it would be really helpful if someone could help me with this", charMax()))
Formatting: long piece of text that I need to format, 
>>> print "Formatting: {0}".format(stringLength("long piece of text that I need to format, but I can't explicitly say how long it is going to be because it is coming from another function that gives me the length of how many characters I can print. So, it would be really helpful if someone could help me with this", charMax()))
Formatting: long piece of text that I n
>>> print "Formatting: {0}".format(stringLength("long piece of text that I need to format, but I can't explicitly say how long it is going to be because it is coming from another function that gives me the length of how many characters I can print. So, it would be really helpful if someone could help me with this", charMax()))
Formatting: long piece of text that I need to format, but I can't expl
>>> print "Formatting: {0}".format(stringLength("long piece of text that I need to format, but I can't explicitly say how long it is going to be because it is coming from another function that gives me the length of how many characters I can print. So, it would be really helpful if someone could help me with this", charMax()))
Formatting: long piece of text that I nee
>>> 

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1124188

Better to put the length in first, and use the .length precision formatting to limit the length:

"Formatting: {1:.{0}s}".format(charMax(), """Some long string""")

Now the string is formatted to a maximum length rather than a minimum:

>>> some_long_string = """\
... long piece of text that I need to format, but I can't explicitly say\
... how long it is going to be because it is coming from another function\
... that gives me the length of how many characters I can print. So, it would be\
... really helpful if someone could help me with this"""
>>> print "Formatting: {1:.{0}s}".format(charMax(), some_long_string)
Formatting: long piece of text that I need to format, bu
>>> print "Formatting: {1:.{0}s}".format(charMax(), some_long_string)
Formatting: long piece of text that I need to format, but I can't explicitly 
>>> print "Formatting: {1:.{0}s}".format(charMax(), some_long_string)
Formatting: long piece of text that I 

It'll work with the orders reversed too:

>>> print "Formatting: {0:.{1}s}".format(some_long_string, charMax())
Formatting: long piece of text that I need to format, but I can't ex

But its clearer for the reader to see what is happening the other way around.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799230

Your formatting specifier is wrong. String length restriction is governed by the precision.

"{0:.{1}s}".format(...)

Upvotes: 1

Related Questions