String formatting in Python

后端 未结 12 1998
-上瘾入骨i
-上瘾入骨i 2020-11-22 08:14

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?

相关标签:
12条回答
  • 2020-11-22 08:50

    You're looking for string formatting, which in python is based on the sprintf function in C.

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

    For a complete reference look here: http://docs.python.org/library/stdtypes.html#string-formatting

    0 讨论(0)
  • 2020-11-22 08:54

    You haven't formulated yourself very commendably, but I'll venture a guess this is what you're looking for:

    foo = "Hello"
    bar = "world"
    baz = 2
    print "%s, %s number %d" % (foo, bar, baz)
    
    0 讨论(0)
  • 2020-11-22 08:55

    I think that this combination is missing :P

    "[{0}, {1}, {2}]".format(*[1, 2, 3])
    
    0 讨论(0)
  • 2020-11-22 08:57

    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]
    
    0 讨论(0)
  • 2020-11-22 08:59

    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)
    
    0 讨论(0)
  • 2020-11-22 08:59

    Since python-3.6, Python supports literal string interpolation [pep-498]. You thus can format with an f prefix to the string. For example:

    x = 1
    y = 2
    z = 3
    f'[{x}, {y}, {z}]'

    This then produces:

    >>> f'[{x}, {y}, {z}]'
    '[1, 2, 3]'
    

    In C# (the language of the String.Format(…)) in the question, since c#-6.0, string interpolation [microsof-tdoc] is supported as well, for example:

    int x = 1;
    int y = 2;
    int z = 3;
    string result = $"[{x}, {y}, {z}]";

    For example:

    csharp> int x = 1;
    csharp> int y = 2;
    csharp> int z = 3;
    csharp> $"[{x}, {y}, {z}]";
    "[1, 2, 3]"
    
    0 讨论(0)
提交回复
热议问题