string-formatting

Python 3 (2.6+) str.format() and regular expressions

Using str.format() is the new standard for formatting strings in Python 2.6, and Python 3. I've run into an issue when using str.format() with regular expressions. I've written a regular expression to return all domains that are a single level below a specified domain or any domains that are 2 levels below the domain specified, if the 2...

Python Format string "}" fill

Python 2.6 defines str.format(…), which, from Python 3.0, is preferred to the old % style of string formatting. Looking at the "mini-language", however, it seems that it is impossible to use the } character as a fill character. This seems like a strange omission to me. Is it possible to somehow escape that character and use it as a fill...

Formatting a String Array to Display to Users

What is the best format to communicate an array of strings in one string to users who are not geeks? I could do it like this: Item1, Item2, Item3 But that becomes meaningless when the strings contain spaces and commas. I could also do it this way: "Item1", "Item2", "Item3" However, I would like to avoid escaping the array elements ...

Python: How can I format a decimal to always show 2 decimal places.

I need it to show: 49 as 49.00 and: 54.9 as 54.90 I need it to have no limitations on length (ie, a trillion dollars), but still always have just 2 decimal places so that it is formatted like money (without the dollar sign): eg, 4898489.00 Is this efficient, BTW? ...

C# string formatting and padding

Seems like this should be something straightforward, but I haven't been able to get it right. I've looked at http://idunno.org/archive/2004/14/01/122.aspx for reference. Example: I would like to print a table of double values, with each double output having 3 decimal precision, and take up 10 spaces (left aligned). Conceptually, I tried...

How to put values of a list into a string

Hello, I am trying to place several values from a list into a string. The code I have is below: ID = [0, 1, 2] print 'ID {0}, {1}, and {2}.'.format(ID) or print (r'(ID\s*=\s*)(\S+)').format(ID) This does not work. Does anyone know where I'm going wrong. The code in the second line prints out the list: [0, 1, 2] the first line...

Combine custom numeric format string with hex format string

I have a situation where I need to format an integer differently depending on whether or not it's zero. Using custom numeric format strings, this can be achieved with the semicolon as a separator. Consider the following: // this works fine but the output is decimal string format = "{0:0000;-0000;''}"; Console.WriteLine(format, 10); // ...

Python, print all floats to 2 decimal places in output

Have a homework assignment that requires I output 4 different floats to two decimal places. This is what I have: print '%.2f' % var1,'kg =','%.2f' % var2,'lb =','%.2f' % var3,'gal =','%.2f' % var4,'l' Which is very unclean, and looks bad. Is there a way to make any float in that out put '%.2f'? Note: Using Python 2.6. ...

J2ME - number format

Hi! I need to format decimal numbers with patterns in J2ME just like java DecimalFormat. I see an option to port Format/NumberFormat/DecimalFormat. Can you suggest ready to use implementation or library? Thanks! ...

how to format 1700 to 1'700 and 1000000 to 1'000'000 in c#?

I like to format all numbers like in math. is there a predefined function or is that just possible with substring and replace? edit: my culture is de-ch Best regards ...

VB6 Format function: analog in .NET

There is String.Format function that is referred to in the documentation as the analog for Format function from VB6. There's also Format function from VisualBasic namespace that is provided for compatibility and basically has same powers as String.Format. Indeed, those two format dates and numbers. But VB6's function was also able to f...

Workout the string format used for DateTime formatting.

I have a string which represents a DataTime value, and I want to workout what string format was used to create the string. For example - Given "Wednesday 27 Jan 2010" I expect "dddd dd MMM yyyy" - Given "2010 01 27" I expect "yyyy MM dd" Assume that the date is close to DateTime.Now and relates to the CurrentCulture. So given that we h...

How do I turn a python datetime into a string, with readable format date?

t = e['updated_parsed'] dt = datetime.datetime(t[0],t[1],t[2],t[3],t[4],t[5],t[6] print dt >>>2010-01-28 08:39:49.000003 How do I turn that into a string?: "January 28, 2010" ...

GWT NumberFormat problems with high precision decimals

Hello everyone. I'm using GWT 2.0 and when I try to use NumberFormat to format a Double the results are not as expected: NumberFormat format = NumberFormat.getFormat( "#.########" ); Double d = new Double("0.256281093911"); format.format(d); formatted string: 0.02147484 As you can see the formatted value is wrong (this can be seen in...

Capturing **vars() pattern in string formatting

I frequently find myself using the following pattern for string formatting. a = 3 b = 'foo' c = dict(mykey='myval') #prints a is 3, b is foo, mykey is myval print('a is {a}, b is {b}, mykey is {c[mykey]}'.format(**vars())) That is, I often have the values I need to print in the local namespace, represented by a call to vars(). As I l...

How do I get the 48 least significant bits of a long formatted as hex?

long number = …; // string should contain exactly 12 characters string leastSignificant48bitsOfNumberAsHex = number.ToString("????") ...

Why does .Net use a rounding algorithm in String.Format that is inconsistent with the default Math.Round() algorithm?

I've noticed the following inconsistency in C#/.NET. I was wondering why it is so. Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.04, Math.Round(1.04, 1)); Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.05, Math.Round(1.05, 1)); Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.06, Math.Round(1.06, 1)); Console.WriteLine("{0,-4:#.0} | {1,...

boolean string formatting in Python?

I see I can't do: "%b %b" % (True, False) in Python. I guessed %b for b(oolean). Is there something like this? ...

Attach a formatter to a UITextField on iPhone

I'm trying to format an UITextField as user types text, for instance to show separator for thousands. I found this web page : http://www.iphonedevsdk.com/forum/iphone-sdk-development/16512-trying-add-commas.html It seems that shouldChangeCharactersInRange: is not a good solution. I thought of a custom UIView where the view would be upd...

Using .NET string formatting, how do I format a string to display blank (empty string) for zero (0)?

I am using a DataBinder.Eval expression in an ASP.NET Datagrid, but I think this question applies to String formatting in .NET in general. The customer has requested that if the value of a string is 0, it should not be displayed. I have the following hack to accomplish this: <%# IIf(DataBinder.Eval(Container.DataItem, "MSDWhole").Trim...