Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get around "Single '}' encountered in format string" when using .format and formatting in printing

Tags:

python

I am currently trying to print a tabulated format (using left alignment and padding) for headings in a table however I keep getting the following error.

ValueError: Single '}' encountered in format string 

Here's the line:

print("{0}:<15}{1}:<15}{2}:<8}".format("1", "2", "3")) 

Required output is something along the lines of:

1              2              3         

I've tried duplicating the { } as advised here but received no luck.

I am probably missing something incredibly obvious however after staring at it for ages, I cannot see it. After all, what's the harm in asking?

Thanks

like image 958
PCJonathan Avatar asked Jan 31 '12 12:01

PCJonathan


People also ask

What is the string .format method used for in Python?

The format() method formats the specified value(s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}. Read more about the placeholders in the Placeholder section below. The format() method returns the formatted string.

What string method is used to format values in a string?

The Java String. format() method returns the formatted string by a given locale, format, and argument. If the locale is not specified in the String.

What is %p format string?

%p expects the argument to be of type (void *) and prints out the address. Whereas %x converts an unsigned int to unsigned hexadecimal and prints out the result.


1 Answers

Use }}:

>>> "{0}:<15}}{1}:<15}}{2}:<8}}".format("1", "2", "3") '1:<15}2:<15}3:<8}' 
like image 130
Fred Foo Avatar answered Oct 20 '22 03:10

Fred Foo