string-formatting

Does Python do variable interpolation similar to “string #{var}” in Ruby?

狂风中的少年 提交于 2019-11-27 11:07:11
问题 In Python, it is tedious to write: print "foo is" + bar + '.' Can I do something like this in Python? print "foo is #{bar}." 回答1: Python 3.6+ does have variable interpolation - prepend an f to your string: f"foo is {bar}" For versions of Python below this (Python 2 - 3.5) you can use str.format to pass in variables: # Rather than this: print("foo is #{bar}") # You would do this: print("foo is {}".format(bar)) # Or this: print("foo is {bar}".format(bar=bar)) # Or this: print("foo is %s" % (bar

Objective-C formatting string for boolean?

浪子不回头ぞ 提交于 2019-11-27 11:01:20
问题 What formatter is used for boolean values? EDIT: Example: NSLog(@" ??", BOOL_VAL); , what is ?? ? 回答1: One way to do it is to convert to strings (since there are only two possibilities, it isn't hard): NSLog(@" %s", BOOL_VAL ? "true" : "false"); I don't think there is a format specifier for boolean values. 回答2: I would recommend NSLog(@"%@", boolValue ? @"YES" : @"NO"); because, um, BOOL s are called YES or NO in Objective-C. 回答3: Use the integer formatter %d , which will print either 0 or 1

Format in kotlin string templates

只愿长相守 提交于 2019-11-27 10:11:55
问题 Kotlin has an excellent feature called string templates. I really love it. val i = 10 val s = "i = $i" // evaluates to "i = 10" But is it possible to have any formatting in the templates? For example, I would like to format Double in string templates in kotlin, at least to set a number of digits after a decimal separator: val pi = 3.14159265358979323 val s = "pi = $pi??" // How to make it "pi = 3.14"? 回答1: Unfortunately, there's no built-in support for formatting in string templates yet, as a

JavaScript Chart.js - Custom data formatting to display on tooltip

只愿长相守 提交于 2019-11-27 09:58:05
问题 I have looked at various documentation and similar questions on here, but cannot seem to find the particular solution. Apologies if I have missed anything obvious or have repeated this question! As a bit of background info, I have implemented 4 graphs using the Chart.js plugin and passed in the required data using PHP from a database. This is all working correctly and is fine. My problem is I need to display the data in the tooltips as formatted data aka. as numeric with %. As an example, one

Matlab file name with zero-padded numbers

纵然是瞬间 提交于 2019-11-27 09:20:17
I have 11x11 matrices and I saved them as .mat files from F01_01 to F11_11 . I have to run a function Func on each file. Since it takes a long time, I want to write a script to run the function automatically: for i=01:11 for j=01:11 filename=['F',num2str(i), '_', num2str(j),'.mat']; load(filename); Func(Fi_j); % run the function for each file Fi_j end end But it doesn't work, Matlab cannot find the mat-files. Could somebody please help ? Shai The problem i=01; j=01; ['F',num2str(i), '_', num2str(j),'.mat'] evaluates to F1_1.mat and not to F01_01.mat as expected. The reason for this is that i

Add custom conversion types for string formatting

时光总嘲笑我的痴心妄想 提交于 2019-11-27 09:18:08
Is there anyway in python to add additional conversion types to string formatting? The standard conversion types used in % -based string formatting are things like s for strings, d for decimals, etc. What I'd like to do is add a new character for which I can specify a custom handler (for instance a lambda function) that will return the string to insert. For instance, I'd like to add h as a conversion type to specify that the string should be escaped for using in HTML. As an example: #!/usr/bin/python print "<title>%(TITLE)h</title>" % {"TITLE": "Proof that 12 < 6"} And this would use cgi

print variable-name in Matlab

狂风中的少年 提交于 2019-11-27 09:14:24
I have a function in Matlab that has some variables in it. I need to print out the variable_names (in case of an exception etc.). I am aware of inputname function but it works for input_arguments only. mat = [ 1 2 ; 3 4 ] ; % disp(['Error in var: ' ??(a)]) % desired ouput: Error in var: mat (and NOT 1 2 ; 3 4!) Thanks! Matlab essentially does not let you do that. However, you can write a helper function to ease your pain in creating output like that: function disp_msg_var(msg, v) disp([msg inputname(2)]); end which you could call like so in your case: disp_msg_var('Error in: ', a); You can

How to use Single Quotes in Eval Format String

£可爱£侵袭症+ 提交于 2019-11-27 07:46:34
问题 I've got a Repeater and its SqlDatasource nested inside a Gridview TemplatedField. The Repeater's datasource SelectCommand is set using the FormatString of an Eval from the Gridview. The SelectCommand has a WHERE clause which is to compare a string. Because I have already used the single and double quotes, I am having trouble delimiting the string in the SQL WHERE clause. How do I add single quotes inside an Eval FormatString? I have tried using 'Replace'. I have tried using 'Special

Java - truncate string from left with formatter flag

时光毁灭记忆、已成空白 提交于 2019-11-27 07:45:42
问题 I have a string, say: String s = "0123456789"; I want to pad it with a formatter. I can do this two ways: String.format("[%1$15s]", s); //returns [ 0123456789] or String.format("[%1$-15s]", s); // returns [0123456789 ] if I want to truncate text I do String.format("[%1$.5s]", s); // returns [01234] if I want to truncate from the left, I thought I could do this: String.format("[%1$-.5s]", s); // throws MissingFormatWidthException but this failed, so I tried this: String.format("[%1$-0.5s]", s)

Is there a “String.Format” that can accept named input parameters instead of index placeholders? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-27 07:43:36
This question already has an answer here: Using variables inside strings 3 answers What's a good way of doing string templating in .NET? 12 answers This is what I know str = String.Format("Her name is {0} and she's {1} years old", "Lisa", "10"); But I want something like str = String("Her name is @name and she's @age years old"); str.addParameter(@name, "Lisa"); str.addParameter(@age, 10); Fabien PERRONNET In C# 6 you can use string interpolation : string name = "Lisa"; int age = 20; string str = $"Her name is {name} and she's {age} years old"; As Doug Clutter mentioned in his comment , string