string-formatting

String with 'f' prefix in python-3.6

。_饼干妹妹 提交于 2019-11-26 04:45:35
问题 I\'m trying out Python 3.6. Going through new code, I stumbled upon this new syntax: f\"My formatting string!\" It seems we can do things like this: >>> name = \"George\" >>> print(f\"My cool string is called {name}.\") My cool string is called George. Can anyone shed some light on the inner workings of this? In particular what is the scope of the variables that an f-prefixed string can take? 回答1: See PEP 498 Literal String Interpolation: The expressions that are extracted from the string are

Format a datetime into a string with milliseconds

三世轮回 提交于 2019-11-26 04:12:29
问题 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= timeformatted.replace(\"-\",\"\") almostformatted= semiformatted.replace(\":\",\"\") formatted=almostformatted.replace(\".\",\"\") withspacegoaway=formatted.replace(\" \",\"\") formattedstripped=withspacegoaway.strip() print formattedstripped 回答1: To get a date string with

Custom numeric format string to always display the sign

Deadly 提交于 2019-11-26 04:09:37
问题 Is there any way I can specify a standard or custom numeric format string to always output the sign, be it +ve or -ve (although what it should do for zero, I\'m not sure!) 回答1: Yes, you can. There is conditional formatting. See Conditional formatting in MSDN eg: string MyString = number.ToString("+0;-#"); Where each section separated by a semicolon represents positive and negative numbers or: string MyString = number.ToString("+#;-#;0"); if you don't want the zero to have a plus sign. 回答2:

Formatting Numbers by padding with leading zeros in SQL Server

女生的网名这么多〃 提交于 2019-11-26 03:51:08
问题 We have an old SQL table that was used by SQL Server 2000 for close to 10 years. In it, our employee badge numbers are stored as char(6) from 000001 to 999999 . I am writing a web application now, and I need to store employee badge numbers. In my new table, I could take the short cut and copy the old table, but I am hoping for better data transfer, smaller size, etc, by simply storing the int values from 1 to 999999 . In C#, I can quickly format an int value for the badge number using public

How to format strings in Java

和自甴很熟 提交于 2019-11-26 03:19:46
问题 Primitive question, but how do I format strings like this: \"Step {1} of {2}\" by substituting variables using Java? In C# it\'s easy. 回答1: In addition to String.format, also take a look java.text.MessageFormat. The format less terse and a bit closer to the C# example you've provided and you can use it for parsing as well. For example: int someNumber = 42; String someString = "foobar"; Object[] args = {new Long(someNumber), someString}; MessageFormat fmt = new MessageFormat("String is \"{1}\"

How can I format a decimal to always show 2 decimal places?

蹲街弑〆低调 提交于 2019-11-26 03:16:38
问题 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 to display money values. eg, 4898489.00 回答1: I suppose you're probably using the Decimal() objects from the decimal module? (If you need exactly two digits of precision beyond the decimal point with arbitrarily large numbers, you definitely

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

爷,独闯天下 提交于 2019-11-26 03:10:02
问题 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\" 回答1: The datetime class has a method strftime. The Python docs documents the different formats it accepts: Python 2: strftime() Behavior Python 3: strftime() Behavior For this specific example, it would look something like: my_datetime.strftime("%B %d, %Y") 回答2: Here is how you can accomplish the same using python's

How to convert milliseconds into human readable form?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 03:04:35
问题 I need to convert an arbitrary amount of milliseconds into Days, Hours, Minutes Second. For example: 10 Days, 5 hours, 13 minutes, 1 second. 回答1: Well, since nobody else has stepped up, I'll write the easy code to do this: x = ms / 1000 seconds = x % 60 x /= 60 minutes = x % 60 x /= 60 hours = x % 24 x /= 24 days = x I'm just glad you stopped at days and didn't ask for months. :) Note that in the above, it is assumed that / represents truncating integer division. If you use this code in a

php Replacing multiple spaces with a single space [duplicate]

时光怂恿深爱的人放手 提交于 2019-11-26 03:04:29
问题 This question already has answers here : How can I convert ereg expressions to preg in PHP? (4 answers) Closed 6 months ago . 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 spaces and multiple nbsp white spaces with a single white space. 回答1: Use preg_replace() and instead of [ \t\n\r]

Best way to format integer as string with leading zeros? [duplicate]

会有一股神秘感。 提交于 2019-11-26 02:27:28
问题 This question already has an answer here: Display number with leading zeros 15 answers 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; $i<($cnt-strlen($int)); $i++) $nulls .= \'0\'; return $nulls.$int; } Is there a function that can do this? 回答1: You can use the zfill() method to pad a string with zeros: In [3