string-formatting

String.Format - how it works and how to implement custom formatstrings

我只是一个虾纸丫 提交于 2019-11-28 04:48:24
With String.Format() it is possible to format for example DateTime objects in many different ways. Every time I am looking for a desired format I need to search around on Internet. Almost always I find an example I can use. For example: String.Format("{0:MM/dd/yyyy}", DateTime.Now); // "09/05/2012" But I don't have any clue how it works and which classes support these 'magic' additional strings. So my questions are: How does String.Format map the additional information MM/dd/yyyy to a string result? Do all Microsoft objects support this feature? Is this documented somewhere? Is it possible to

String format with optional dict key-value

烈酒焚心 提交于 2019-11-28 04:25:51
问题 Is there any way to format string with dict but optionally without key errors? This works fine: opening_line = '%(greetings)s %(name)s !!!' opening_line % {'greetings': 'hello', 'name': 'john'} But let's say I don't know the name, and I would like to format above line only for 'greetings' . Something like, opening_line % {'greetings': 'hello'} Output would be fine even if: 'hii %(name)s !!!' # keeping name un-formatted But this gives KeyError while unpacking Is there any way? 回答1: Use

Using Python String Formatting with Lists

可紊 提交于 2019-11-28 03:52:48
I construct a string s in Python 2.6.5 which will have a varying number of %s tokens, which match the number of entries in list x . I need to write out a formatted string. The following doesn't work, but indicates what I'm trying to do. In this example, there are three %s tokens and the list has three entries. s = '%s BLAH %s FOO %s BAR' x = ['1', '2', '3'] print s % (x) I'd like the output string to be: 1 BLAH 2 FOO 3 BAR print s % tuple(x) instead of print s % (x) You should take a look to the format method of python. You could then define your formatting string like this : >>> s = '{0} BLAH

How do I format a number with a variable number of digits in Python? [duplicate]

妖精的绣舞 提交于 2019-11-28 03:27:04
This question already has an answer here: Format string dynamically 5 answers Say I wanted to display the number 123 with a variable number of padded zeroes on the front. For example, if I wanted to display it in 5 digits I would have digits = 5 giving me: 00123 If I wanted to display it in 6 digits I would have digits = 6 giving: 000123 How would I do this in Python? There is a string method called zfill: >>> '12344'.zfill(10) 0000012344 It will pad the left side of the string with zeros to make the string length N (10 in this case). If you are using it in a formatted string with the format()

Format a JavaScript string using placeholders and an object of substitutions?

白昼怎懂夜的黑 提交于 2019-11-28 03:22:38
I have a string with say: My Name is %NAME% and my age is %AGE%. %XXX% are placeholders. We need to substitute values there from an object. Object looks like: {"%NAME%":"Mike","%AGE%":"26","%EVENT%":"20"} I need to parse the object and replace the string with corresponding values. So that final output will be: My Name is Mike and my age is 26. The whole thing has to be done either using pure javascript or jquery. The requirements of the original question clearly couldn't benefit from string interpolation, as it seems like it's a runtime processing of arbitrary replacement keys. However , if

Reuse a parameter in String.format?

ぐ巨炮叔叔 提交于 2019-11-28 03:17:00
String hello = "Hello"; String.format("%s %s %s %s %s %s", hello, hello, hello, hello, hello, hello); hello hello hello hello hello hello Does the hello variable need to be repeated multiple times in the call to the format method or is there a shorthand version that lets you specify the argument once to be applied to all of the %s tokens? From the docs : The format specifiers for general, character, and numeric types have the following syntax: %[argument_index$][flags][width][.precision]conversion The optional argument_index is a decimal integer indicating the position of the argument in the

Python add leading zeroes using str.format [duplicate]

二次信任 提交于 2019-11-28 03:11:21
This question already has an answer here: Best way to format integer as string with leading zeros? [duplicate] 10 answers Can you display an integer value with leading zeroes using the str.format function? Example input: "{0:some_format_specifying_width_3}".format(1) "{0:some_format_specifying_width_3}".format(10) "{0:some_format_specifying_width_3}".format(100) Desired output: "001" "010" "100" I know that both zfill and % -based formatting (e.g. '%03d' % 5 ) can accomplish this. However, I would like a solution that uses str.format in order to keep my code clean and consistent (I'm also

Is it more Pythonic to use String Formatting over String Concatenation in Python 3?

一曲冷凌霜 提交于 2019-11-28 01:54:04
问题 So I'm programming a text game in Python 3.4 that requires the use of the print() function very often to display variables to the user. The two ways I've always done this is with string formatting and string concatenation : print('{} has {} health left.'.format(player, health)) And, print(player + ' has ' + str(health) + ' health left.') So which is better? They're both equally as readable and quick to type, and perform exactly the same. Which one is more Pythonic and why? Question asked as I

Concatenate int and string

被刻印的时光 ゝ 提交于 2019-11-28 01:44:49
How can I concatenate an int (e.g: 4 ) and a string (e.g: @"/12" ) for printing? I'm okay with casting the int it to an NSString format, but I don't know how I can add @"/12" after the int's value. I don't get your question, but I think you mean something like this: ...[NSString stringWithFormat:@"%d/12", intValue] 来源: https://stackoverflow.com/questions/5884367/concatenate-int-and-string

Correct format specifier for return value of sizeof() in C

≯℡__Kan透↙ 提交于 2019-11-28 00:33:01
I have the following code: #include<stdio.h> int main() { printf("The 'int' datatype is \t\t %lu bytes\n", sizeof(int)); printf("The 'unsigned int' data type is\t %lu bytes\n", sizeof(unsigned int)); printf("The 'short int' data type is\t %lu bytes\n", sizeof(short int)); printf("The 'long int' data type is\t %lu bytes\n", sizeof(long int)); printf("The 'long long int' data type is %lu bytes\n", sizeof(long long int)); printf("The 'float' data type is\t %lu bytes\n", sizeof(float)); printf("The 'char' data type is\t\t %lu bytes\n", sizeof(char)); } Which outputs: The 'int' datatype is 4 bytes