string-formatting

How do I use string formatting to show BOTH leading zeros and precision of 3?

孤街浪徒 提交于 2020-08-20 23:48:26
问题 I'm trying to represent a number with leading and trailing zeros so that the total width is 7 including the decimal point. For example, I want to represent "5" as "005.000". It seems that string formatting will let me do one or the other but not both. Here's the output I get in Ipython illustrating my problem: In [1]: '%.3f'%5 Out[1]: '5.000' In [2]: '%03.f'%5 Out[2]: '005' In [3]: '%03.3f'%5 Out[3]: '5.000' Line 1 and 2 are doing exactly what I would expect. Line 3 just ignores the fact that

format strings and named arguments in Python

烂漫一生 提交于 2020-08-20 19:14:04
问题 Case 1: "{arg1} {arg2}".format (10, 20) It will give KeyError: 'arg1' because I didn't pass the named arguments. Case 2: "{arg1} {arg2}".format(arg1 = 10, arg2 = 20) Now it will work properly because I passed the named arguments. And it prints '10 20' Case 3: And, If I pass wrong name it will show KeyError: 'arg1' "{arg1} {arg2}".format(wrong = 10, arg2 = 20) But, Case 4: If I pass the named arguments in wrong order "{arg1} {arg2}".format(arg2 = 10, arg1 = 20) It works... and it prints '20 10

format strings and named arguments in Python

感情迁移 提交于 2020-08-20 19:12:41
问题 Case 1: "{arg1} {arg2}".format (10, 20) It will give KeyError: 'arg1' because I didn't pass the named arguments. Case 2: "{arg1} {arg2}".format(arg1 = 10, arg2 = 20) Now it will work properly because I passed the named arguments. And it prints '10 20' Case 3: And, If I pass wrong name it will show KeyError: 'arg1' "{arg1} {arg2}".format(wrong = 10, arg2 = 20) But, Case 4: If I pass the named arguments in wrong order "{arg1} {arg2}".format(arg2 = 10, arg1 = 20) It works... and it prints '20 10

How can I use C++20 std::format?

不打扰是莪最后的温柔 提交于 2020-08-02 16:55:13
问题 C++20 introduces std::format . What are the advantages over printf or std::cout . How can I use it and someone give an example of it? 回答1: What are the advantages over printf Type safety. For printf, the programmer must carefully match the format specifier to the type of the argument. If they make a mistake, the behaviour of the program is undefined. This is a very common source of bugs, especially for beginners. To be fair, decent compilers diagnose these mistakes as long as a constant

f-string syntax for unpacking a list with brace suppression

爷,独闯天下 提交于 2020-07-04 08:33:42
问题 I have been examining some of my string format options using the new f-string format. I routinely need to unpack lists and other iterables of unknown length. Currently I use the following... >>> a = [1, 'a', 3, 'b'] >>> ("unpack a list: " + " {} "*len(a)).format(*a) 'unpack a list: 1 a 3 b ' This, albeit a bit cumbersome, does the job using pre-3.6 .format notation. The new f-string format option is interesting given runtime string concatenation. It is the replication of the number of {} that

f-string syntax for unpacking a list with brace suppression

六月ゝ 毕业季﹏ 提交于 2020-07-04 08:33:07
问题 I have been examining some of my string format options using the new f-string format. I routinely need to unpack lists and other iterables of unknown length. Currently I use the following... >>> a = [1, 'a', 3, 'b'] >>> ("unpack a list: " + " {} "*len(a)).format(*a) 'unpack a list: 1 a 3 b ' This, albeit a bit cumbersome, does the job using pre-3.6 .format notation. The new f-string format option is interesting given runtime string concatenation. It is the replication of the number of {} that

String Formatting in Python/Thunderbird

强颜欢笑 提交于 2020-06-29 03:40:17
问题 Noob, trying to use Thunderbird (rather than SMTP) to send personalized emails to a few dozen people. I am basically looking to have the message display in Thunderbird as follows: Dear Bob, It was nice to meet you the other day. However, I instead end up with: Dear Bob (comma missing, and rest of body missing) I have tried the following: import subprocess import os def send_email(name, email_address): #print(name, email_address) os.system("thunderbird -compose to= 'to',subject='subject',body=

How can I format a std::string using a collection of arguments?

大憨熊 提交于 2020-06-09 16:49:48
问题 Is it possible to format std::string passing a set of arguments? Currently I am formatting the string this way: string helloString = "Hello %s and %s"; vector<string> tokens; //initialized vector of strings const char* helloStringArr = helloString.c_str(); char output[1000]; sprintf_s(output, 1000, helloStringArr, tokens.at(0).c_str(), tokens.at(1).c_str()); But the size of the vector is determined at runtime. Is there any similar function to sprintf_s which takes a collection of arguments

Java output double numbers in exponential format

半世苍凉 提交于 2020-05-13 06:11:25
问题 I have some double numbers that are outputted with this format: Format.String("%1.4e",doubleNumber); The result is 1.123456e+03 . How can I set the number of cipher of exponent for getting this format: 1.123456e+003 I would have always 3 cipher after e symbol. Thank you UPDATE 1: I have partially resolved: DecimalFormat formatter = new DecimalFormat("0.000000E000"); System.out.println( formatter.format(doubleNumber) ); Now the number has always the format 1.123456e0xx or 1.123456e-0xx But it