Questions tagged [string-formatting]

Commonly refers to a number of methods to display an arbitrary number of varied data types into a string.

The most common type of string formatting is printf style.

4270 questions
2427
votes
23 answers

How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?

Non-working example: print(" \{ Hello \} {0} ".format(42)) Desired output: {Hello} 42
Schitti
  • 25,489
  • 8
  • 24
  • 21
1448
votes
16 answers

String formatting: % vs. .format vs. f-string literal

There are various string formatting methods: Python <2.6: "Hello %s" % name Python 2.6+: "Hello {}".format(name)   (uses str.format) Python 3.6+: f"{name}"   (uses f-strings) Which is better, and for what situations? The following methods have…
NorthIsUp
  • 17,502
  • 9
  • 29
  • 35
1373
votes
19 answers

Display number with leading zeros

How do I display a leading zero for all numbers with less than two digits? 1 → 01 10 → 10 100 → 100
ashchristopher
  • 25,143
  • 18
  • 48
  • 49
739
votes
14 answers

How can I fill out a Python string with spaces?

I want to fill out a string with spaces. I know that the following works for zero's: >>> print "'%06d'"%4 '000004' But what should I do when I want this?: 'hi ' of course I can measure string length and do str+" "*leftover, but I'd like the…
taper
  • 9,236
  • 5
  • 28
  • 29
646
votes
14 answers

Is it possible to have placeholders in strings.xml for runtime values?

Is it possible to have placeholders in string values in string.xml that can be assigned values at run time? Example: some string PLACEHOLDER1 some more string
SoftReference
  • 6,969
  • 4
  • 19
  • 25
551
votes
8 answers

Format a Go string without printing?

Is there a simple way to format a string in Go without printing the string? I can do: bar := "bar" fmt.Printf("foo: %s", bar) But I want the formatted string returned rather than printed so I can manipulate it further. I could also do something…
Carnegie
  • 5,645
  • 2
  • 15
  • 7
429
votes
10 answers

Best way to format integer as string with leading zeros?

I need to add leading zeros to integer to make a string with defined quantity of digits ($cnt). What the best way to translate this simple function from PHP to Python: function add_nulls($int, $cnt=2) { $int = intval($int); for($i=0;…
ramusus
  • 7,789
  • 5
  • 38
  • 45
391
votes
18 answers

Using String Format to show decimal up to 2 places or simple integer

I have got a price field to display which sometimes can be either 100 or 100.99 or 100.9, What I want is to display the price in 2 decimal places only if the decimals are entered for that price , for instance if its 100 so it should only show 100…
Mr A
  • 6,448
  • 25
  • 83
  • 137
364
votes
13 answers

How can I format a decimal to always show 2 decimal places?

I want to display: 49 as 49.00 and: 54.9 as 54.90 Regardless of the length of the decimal or whether there are are any decimal places, I would like to display a Decimal with 2 decimal places, and I'd like to do it in an efficient way. The purpose is…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
352
votes
15 answers

Format a datetime into a string with milliseconds

How can I format a datetime object as a string with milliseconds?
Jurudocs
  • 8,595
  • 19
  • 64
  • 88
332
votes
11 answers

How can I format a number into a string with leading zeros?

I have a number that I need to convert to a string. First I used this: Key = i.ToString(); But I realize it's being sorted in a strange order and so I need to pad it with zeros. How could I do this?
Mandy Weston
  • 3,435
  • 2
  • 17
  • 6
312
votes
10 answers

Kotlin – String formatting

Kotlin has an excellent feature called string templates. val i = 10 val s = "i = $i" // evaluates to "i = 10" But is it possible to have any formatting in the templates? For example, I would like to format Double in string templates in kotlin, at…
MajesticRa
  • 13,770
  • 12
  • 63
  • 77
281
votes
12 answers

How to format strings in Java

Primitive question, but how do I format strings like this: "Step {1} of {2}" by substituting variables using Java? In C# it's easy.
katit
  • 17,375
  • 35
  • 128
  • 256
247
votes
13 answers

Display a decimal in scientific notation

How can I display Decimal('40800000000.00000000000000') as '4.08E+10'? I've tried this: >>> '%E' % Decimal('40800000000.00000000000000') '4.080000E+10' But it has those extra 0's.
Greg
  • 45,306
  • 89
  • 231
  • 297
246
votes
13 answers

Should I use Java's String.format() if performance is important?

We have to build Strings all the time for log output and so on. Over the JDK versions we have learned when to use StringBuffer (many appends, thread safe) and StringBuilder (many appends, non-thread-safe). What's the advice on using String.format()?…
Air
  • 5,084
  • 5
  • 25
  • 19
1
2 3
99 100