string-formatting

What does 'h' mean in a python format string?

大城市里の小女人 提交于 2019-12-10 14:58:33
问题 This is a valid python format string: >>> wierd_format = '[%27he]' >>> print wierd_format % 2.5 [ 2.500000e+00] But this isn't: >>> bad_format = '[%20qe]' >>> print bad_format % 2.5 Traceback (most recent call last): File "prog.py", line 5, in <module> print bad_format % 2.5 ValueError: unsupported format character 'q' (0x71) at index 4 Clearly, h is a supported format character. However, the documentation doesn't mention an h specifier. What does it do? 回答1: From the docs: A length modifier

String format printing with python3: print from unpacked array *some* of the time

岁酱吖の 提交于 2019-12-10 14:54:36
问题 In my question a few minutes ago, I asked about how to print using python's str.format printing, when the strings are stored in an array. Then answer was clearly unpack the list, like this: # note that I had to play with the whitespace because the {} text is 2 characters, while its replacement is always one hex_string = r''' _____ / \ / \ ,----( {} )----. / \ / \ / {} \_____/ {} \ \ / \ / \ / \ / )----( {} )----( / \ / \ / \_____/ \ \ {} / \ {} / \ / \ / `----( {} )----' \ / \_____/ '''

Help in padding numerical strings

橙三吉。 提交于 2019-12-10 14:53:25
问题 I am trying to generate a numerical string by padding the number with zeroes to the left. 0 would become 00000 1 would become 00001 10 would become 00010 I want to create five character NSString by padding the number with zeroes. I read this Create NSString by repeating another string a given number of times but the output is an NSMutableString . How can I implement this algorithm with the output as an NSString ? Best regards. 回答1: You can accomplish this by calling [NSString stringWithFormat

Why would I ever use anything else than %r in Python string formatting?

こ雲淡風輕ζ 提交于 2019-12-10 14:53:20
问题 I occasionally use Python string formatting. This can be done like so: print('int: %i. Float: %f. String: %s' % (54, 34.434, 'some text')) But, this can also be done like this: print('int: %r. Float: %r. String: %r' % (54, 34.434, 'some text')) As well as using %s: print('int: %s. Float: %s. String: %s' % (54, 34.434, 'some text')) My question is therefore: why would I ever use anything else than the %r or %s? The other options (%i, %f and %s) simply seem useless to me, so I'm just wondering

Take next parameter as field width in String.Format

陌路散爱 提交于 2019-12-10 14:19:02
问题 In C#, I have a width I want to use for some strings, but I won't know that width until runtime. I'm doing something like this: string.Format("{0, " + digits + "}", value) // prints 123 as " 123" Is there a string formatting directive that lets me specify this without smashing my own format string together like this? I looked around on MSDN for a little while and I feel like I'm missing a whole chapter on format strings or something. 回答1: Take a look at PadLeft : s = "123".PadLeft(5); //

compact Number formatting behavior in Java (automatically switch between decimal and scientific notation)

こ雲淡風輕ζ 提交于 2019-12-10 14:00:15
问题 I am looking for a way to format a floating point number dynamically in either standard decimal format or scientific notation, depending on the value of the number. For moderate magnitudes, the number should be formatted as a decimal with trailing zeros suppressed. If the floating point number is equal to an integral value, the decimal point should also be suppressed. For extreme magnitudes (very small or very large), the number should be expressed in scientific notation. Alternately stated,

How to apply coloring/formatting to the displayed text in input()-function (similar to print statement formatting)?

落爺英雄遲暮 提交于 2019-12-10 13:17:14
问题 I have a small game application, which is started from the Windows console (cmd.exe). I am able to format the text in any desired way using ANSI escape sequences. I would also love to apply formatting to the text from the input() -method, but I have not found a way how to do so. Here is the testing code... from colorama import init init(autoreset=True) RED = "\x1b[1;31;40m" print(f"{RED}This text is red\n") not_red = input(f"{RED}Insert some random stuff: ") in my windows console, you will

How to show a comma separated number with StringFormat in XAML?

女生的网名这么多〃 提交于 2019-12-10 12:49:46
问题 My code currently shows like this: 43521 reviews , I want it be like this: 43,521 reviews . How can I do that? and is there a full reference for all possible formats in StringFormat ? couldn't find anything. thanks. <TextBlock Text="{Binding Reviews,StringFormat='{}{0} reviews'}"/> 回答1: just change your string format like this: <TextBlock Text="{Binding Reviews,StringFormat='{}{0:0,0} reviews'}"/> 回答2: This one also worked :) <TextBlock Text="{Binding Reviews,StringFormat='{}{0:N0} reviews'}"

Python string formatting with percent sign

别来无恙 提交于 2019-12-10 12:40:24
问题 I am trying to do exactly the following: >>> x = (1,2) >>> y = 'hello' >>> '%d,%d,%s' % (x[0], x[1], y) '1,2,hello' However, I have a long x , more than two items, so I tried: >>> '%d,%d,%s' % (*x, y) but it is syntax error. What would be the proper way of doing this without indexing like the first example? 回答1: str % .. accepts a tuple as a right-hand operand, so you can do the following: >>> x = (1, 2) >>> y = 'hello' >>> '%d,%d,%s' % (x + (y,)) # Building a tuple of `(1, 2, 'hello')` '1,2

C#: Formatting Price value string

我们两清 提交于 2019-12-10 12:39:49
问题 in C#,I have a double variable price with value 10215.24. I want to show the price with comma after some digits. My expected output is 10,215.24 回答1: myPrice.ToString("N2"); depending on what you want, you may also wish to display the currency symbol: myPrice.ToString("C2"); (The number after the C or N indicates how many decimals should be used). (C formats the number as a currency string, which includes a currency symbol) To be completely politically correct, you can also specify the