Using Python string formatting with lists

I am building a string s in Python 2.6.5, which will have a different number of %s tokens that correspond to the number of entries in list x . I need to write a formatted string. The following work does not work, but indicates what I'm trying to do. In this example, there are three %s tags, and the list consists of three entries.

 s = '%s BLAH %s FOO %s BAR' x = ['1', '2', '3'] print s % (x) 

I need an output line:

1 BLAH 2 FOO 3 BAR

+90
python string list formatting string-formatting
Sep 27 '11 at 11:51
source share
6 answers
 print s % tuple(x) 

instead

 print s % (x) 
+101
Sep 27 '11 at 11:53 on
source share

You should take a look at the python format method. You can then define the format string as follows:

 >>> s = '{0} BLAH {1} BLAH BLAH {2} BLAH BLAH BLAH' >>> x = ['1', '2', '3'] >>> print s.format(*x) '1 BLAH 2 BLAH BLAH 3 BLAH BLAH BLAH' 
+130
Sep 27 '11 at 11:56 on
source share

Following this resource page, if the length x changes, we can use:

 ', '.join(['%.2f']*len(x)) 

to create a place for each item from the list x . Here is an example:

 x = [1/3.0, 1/6.0, 0.678] s = ("elements in the list are ["+', '.join(['%.2f']*len(x))+"]") % tuple(x) print s >>> elements in the list are [0.33, 0.17, 0.68] 
+22
Jan 09 '15 at 8:53
source share

Since I just found out about this cool thing (indexing into lists from a format string), I am adding to this old question.

 s = '{x[0]} BLAH {x[1]} FOO {x[2]} BAR' x = ['1', '2', '3'] print s.format (x=x) 

However, I still have not figured out how to do the slicing (inside the format string '"{x[2:4]}".format... ), and I would like to understand if anyone has an idea, however, I suspect that you simply cannot do this.

+16
Jun 26 '15 at 19:31
source share

That was a fun question! Another way to handle this for variable-length lists is to create a function that fully implements the .format method and unpacks the lists. In the following example, I do not use unusual formatting, but it can be easily changed to suit your needs.

 list_1 = [1,2,3,4,5,6] list_2 = [1,2,3,4,5,6,7,8] # Create a function that can apply formatting to lists of any length: def ListToFormattedString(alist): # Create a format spec for each item in the input 'alist'. # Eg, each item will be right-adjusted, field width=3. format_list = ['{:>3}' for item in alist] # Now join the format specs into a single string: # Eg, '{:>3}, {:>3}, {:>3}' if the input list has 3 items. s = ','.join(format_list) # Now unpack the input list 'alist' into the format string. Done! return s.format(*alist) # Example output: >>>ListToFormattedString(list_1) ' 1, 2, 3, 4, 5, 6' >>>ListToFormattedString(list_2) ' 1, 2, 3, 4, 5, 6, 7, 8' 
+9
Apr 26 '17 at 17:58
source share

Here is one line. A small impromptu response to using a format with print () in a list.

How about this: (python 3.x)

 sample_list = ['cat', 'dog', 'bunny', 'pig'] print("Your list of animals are: {}, {}, {} and {}".format(*sample_list)) 

Read the docs here about using format () .

+9
Mar 08 '18 at 21:23
source share



All Articles