Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String formatting in Python

I want to do something like String.Format("[{0}, {1}, {2}]", 1, 2, 3) which returns:

[1, 2, 3] 

How do I do this in Python?

like image 205
Joan Venge Avatar asked Feb 05 '09 18:02

Joan Venge


People also ask

What is the new string format in Python?

The format string syntax has become more powerful without complicating the simpler use cases. It pays off to read up on this string formatting mini-language in the Python documentation. In Python 3, this “new style” string formatting is to be preferred over % -style formatting.

How to format string literals in Python?

Starting with Python 3.6, there’s yet another way to format your strings. I’ll tell you all about it in the next section. Python 3.6 added a new string formatting approach called formatted string literals or “f-strings”. This new way of formatting strings lets you use embedded Python expressions inside string constants.

How do I create an F-string in Python?

To create an f-string, prefix the string with the letter “ f ”. The string itself can be formatted in much the same way that you would with str.format (). F-strings provide a concise and convenient way to embed python expressions inside string literals for formatting.

What are the different ways of string formatting?

There are three different ways to perform string formatting:- 1 Formatting with placeholders. 2 Formatting with.format () string method. 3 Formatting with string literals, called f-strings.


2 Answers

The previous answers have used % formatting, which is being phased out in Python 3.0+. Assuming you're using Python 2.6+, a more future-proof formatting system is described here:

http://docs.python.org/library/string.html#formatstrings

Although there are more advanced features as well, the simplest form ends up looking very close to what you wrote:

>>> "[{0}, {1}, {2}]".format(1, 2, 3) [1, 2, 3] 
like image 200
DNS Avatar answered Sep 22 '22 14:09

DNS


You can do it three ways:


Use Python's automatic pretty printing:

print [1, 2, 3]   # Prints [1, 2, 3] 

Showing the same thing with a variable:

numberList = [1, 2] numberList.append(3) print numberList   # Prints [1, 2, 3] 

Use 'classic' string substitutions (ala C's printf). Note the different meanings here of % as the string-format specifier, and the % to apply the list (actually a tuple) to the formatting string. (And note the % is used as the modulo(remainder) operator for arithmetic expressions.)

print "[%i, %i, %i]" % (1, 2, 3) 

Note if we use our pre-defined variable, we'll need to turn it into a tuple to do this:

print "[%i, %i, %i]" % tuple(numberList) 

Use Python 3 string formatting. This is still available in earlier versions (from 2.6), but is the 'new' way of doing it in Py 3. Note you can either use positional (ordinal) arguments, or named arguments (for the heck of it I've put them in reverse order.

print "[{0}, {1}, {2}]".format(1, 2, 3) 

Note the names 'one' ,'two' and 'three' can be whatever makes sense.)

print "[{one}, {two}, {three}]".format(three=3, two=2, one=1) 
like image 34
2 revs, 2 users 99% Avatar answered Sep 24 '22 14:09

2 revs, 2 users 99%