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.

3977 questions
1817
votes
20 answers

How can I print literal curly-brace characters in a string and also use .format on it?

x = " \{ Hello \} {0} " print(x.format(42)) gives me : Key Error: Hello\\ I want to print the output: {Hello} 42
Schitti
  • 19,309
  • 6
  • 21
  • 21
1389
votes
16 answers

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

Python 2.6 introduced the str.format() method with a slightly different syntax from the existing % operator. Which is better and for what situations? Python 3.6 has now introduced another string formatting format of string literals (aka "f" strings)…
NorthIsUp
  • 16,714
  • 8
  • 26
  • 30
1076
votes
17 answers

Display number with leading zeros

Given: a = 1 b = 10 c = 100 How do I display a leading zero for all numbers with less than two digits? This is the output I'm expecting: 01 10 100
ashchristopher
  • 21,525
  • 16
  • 44
  • 49
582
votes
13 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
  • 7,340
  • 4
  • 24
  • 28
552
votes
13 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,039
  • 4
  • 16
  • 24
437
votes
7 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
  • 4,505
  • 2
  • 13
  • 7
358
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
  • 6,209
  • 5
  • 33
  • 44
328
votes
17 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
  • 5,778
  • 23
  • 76
  • 133
281
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
  • 2,925
  • 2
  • 15
  • 6
278
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
  • 48,267
  • 47
  • 159
  • 244
238
votes
7 answers

Format in kotlin string templates

Kotlin has an excellent feature called string templates. I really love it. 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…
MajesticRa
  • 12,389
  • 11
  • 54
  • 71
227
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
  • 4,764
  • 5
  • 23
  • 19
219
votes
14 answers

Format a datetime into a string with milliseconds

I want to have a datetime string from the date with milliseconds. This code is typical for me and I'm eager to learn how to shorten it. from datetime import datetime timeformatted= str(datetime.utcnow()) semiformatted=…
Jurudocs
  • 6,505
  • 16
  • 53
  • 84
211
votes
3 answers

php Replacing multiple spaces with a single space

I'm trying to replace multiple spaces with a single space. When I use ereg_replace, I get an error about it being deprecated. ereg_replace("[ \t\n\r]+", " ", $string); Is there an identical replacement for it. I need to replace multiple " " white…
Dani
  • 2,113
  • 2
  • 13
  • 4
203
votes
10 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
  • 15,901
  • 32
  • 116
  • 238
1
2 3
99 100