Python: Logging TypeError: not all arguments converted during string formatting

You cannot use new-style formatting when using the logging module; use %s instead of {}. logging.info(‘date=%s’, date) The logging module uses the old-style % operator to format the log string. See the debug method for more detail. If you really want to use str.format() string formatting, consider using custom objects that apply the formatting ‘late’, … Read more

f-strings vs str.format()

I’m afraid that it will be deprecated during the next Python versions Don’t be, str.format does not appear (nor has a reason) to be leaving any time soon, the PEP that introduced fprefixed-strings even states in its Abstract: This PEP does not propose to remove or deprecate any of the existing string formatting mechanisms. Formatted … Read more

How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?

You need to double the {{ and }}: >>> x = ” {{ Hello }} {0} ” >>> print(x.format(42)) ‘ { Hello } 42 ‘ Here’s the relevant part of the Python documentation for format string syntax: Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is … Read more

Format a string into columns

You can specify the number of columns occupied by the text as well as alignment using Console.WriteLine or using String.Format: // Prints “–123 –” Console.WriteLine(“–{0,-10}–“, 123); // Prints “– 123–” Console.WriteLine(“–{0,10}–“, 123); The number specifies the number of columns you want to use and the sign specifies alignment (- for left alignment, + for right … Read more

String Format Numbers Thousands 123K, Millions 123M, Billions 123B

There are different ways to achieve this, but for me the easiest and quickest is to use the “,” custom specifier double value = 1234567890; // Displays 1,234,567,890 Console.WriteLine(value.ToString(“#,#”, CultureInfo.InvariantCulture)); // Displays 1,234,568K Console.WriteLine(value.ToString(“#,##0,K”, CultureInfo.InvariantCulture)); // Displays 1,235M Console.WriteLine(value.ToString(“#,##0,,M”, CultureInfo.InvariantCulture)); // Displays 1B Console.WriteLine(value.ToString(“#,##0,,,B”, CultureInfo.InvariantCulture));

In C#, what is the best method to format a string as XML?

string unformattedXml = “<?xml version=\”1.0\”?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>”; string formattedXml = XElement.Parse(unformattedXml).ToString(); Console.WriteLine(formattedXml); Output: <book> <author>Lewis, C.S.</author> <title>The Four Loves</title> </book> The Xml Declaration isn’t output by ToString(), but it is by Save() … XElement.Parse(unformattedXml).Save(@”C:\doc.xml”); Console.WriteLine(File.ReadAllText(@”C:\doc.xml”)); Output: <?xml version=”1.0″ encoding=”utf-8″?> <book> <author>Lewis, C.S.</author> <title>The Four Loves</title> </book>

Implementing String.Format() in VB6

I couldn’t find one anywhere, so I made my own: Public PADDING_CHAR As String Public Function StringFormat(format_string As String, ParamArray values()) As String ‘VB6 implementation of .net String.Format(), slightly customized. ‘Tested with Office 2010 VBA (x64) Dim return_value As String Dim values_count As Integer ‘some error-handling constants: Const ERR_FORMAT_EXCEPTION As Long = vbObjectError Or 9001 … Read more