string-formatting

TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

孤人 提交于 2019-12-17 23:38:31
问题 So I am VERY new to programming and I started with Python 3. I started reading "Learn Python the Hard Way". Now, I got to a point where I had this code: x = "There are %d types of people." % 10 binary = "binary" do_not = "don't" y = "Those who know %s and those who %s" % (binary, do_not) print(x) print(y) print("I said: %r") % x I do not really know the difference between %r , %s and %d . The error I get is TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' No idea what to do

Remove More Than 2 Trailing zero

扶醉桌前 提交于 2019-12-17 21:29:05
问题 I have read many question in stack overflow, what I want is remove 2 or more than two trailing zero behind the decimal. i.e: 12.00 ==> 12 12.30 ==> 12.30 12.35 ==> 12.35 12.345678 ==> 12.34 回答1: NSNumberFormatter *twoDecimalPlacesFormatter = [[[NSNumberFormatter alloc] init] autorelease]; [twoDecimalPlacesFormatter setMaximumFractionDigits:2]; [twoDecimalPlacesFormatter setMinimumFractionDigits:0]; return [twoDecimalPlacesFormatter stringFromNumber:number]; 回答2: I like @dorada's answer, here

How can i use f-string with a variable, not with a string literal?

北城以北 提交于 2019-12-17 20:58:25
问题 I want to use f-string with my string variable, not with string defined with a string literal, "..." . here is my code name=["deep","mahesh","nirbhay"] user_input = r"certi_{element}" # this string i ask from user for element in name: print(f"{user_input}") This code gives output: certi_{element} certi_{element} certi_{element} But I want: certi_{deep} certi_{mahesh} certi_{nirbhay} how can I do this? 回答1: f"..." strings are great when interpolating expression results into a literal , but you

Using a custom formatter in a DataGridView

我只是一个虾纸丫 提交于 2019-12-17 19:52:44
问题 So, maybe this is a bad design; I don't know. But say I have a DataTable with a column that holds int values; these values are in fact meant to represent some enum type that I have in my project. What I'd like to do is have a DataGridView bound to this table and have the column display the name of the enum rather than the integer value "0" or "1" or whatever. One option I considered was to do the whole normalization thing: add a table in the DataSet with the enum names in it, keyed on the

Python: String Formatter Align center

我的未来我决定 提交于 2019-12-17 19:41:40
问题 print('%24s' % "MyString") # prints right aligned print('%-24s' % "MyString") # prints left aligned How do I print it in the center? Is there a quick way to do this? I don't want the text to be in the center of my screen. I want it to be in the center of that 24 spaces. If I have to do it manually, what is the math behind adding the same no. of spaces before and after the text? 回答1: Use the new-style format method instead of the old-style % operator, which doesn't have the centering

Formatting binary values in Scala

╄→尐↘猪︶ㄣ 提交于 2019-12-17 18:15:35
问题 Does Scala have a built in formatter for binary data? For example to print out: 00000011 for the Int value 3. Writing one won't be difficult - just curious if it exists. 回答1: scala> 3.toBinaryString res0: String = 11 Scala has an implicit conversion from Int to RichInt which has a method toBinaryString. This function does not print the leading zeroes though. 回答2: I don't know of a direct API method to do it, but here is one way of doing it: def toBinary(i: Int, digits: Int = 8) = String

“%s” % format vs “{0}”.format() vs “?” format

余生颓废 提交于 2019-12-17 18:07:17
问题 In this post about SQLite, aaronasterling told me that cmd = "attach \"%s\" as toMerge" % "b.db" : is wrong cmd = 'attach "{0}" as toMerge'.format("b.db") : is correct cmd = "attach ? as toMerge"; cursor.execute(cmd, ('b.db', )) : is right thing But, I've thought the first and second are the same. What are the differences between those three? 回答1: "attach \"%s\" as toMerge" % "b.db" You should use ' instead of " , so you don't have to escape. You used the old formatting strings that are

Get the first integers in a string with JavaScript

旧时模样 提交于 2019-12-17 17:32:29
问题 I have a string in a loop and for every loop, it is filled with texts the looks like this: "123 hello everybody 4" "4567 stuff is fun 67" "12368 more stuff" I only want to retrieve the first numbers up to the text in the string and I, of course, do not know the length. Thanks in advance! 回答1: If the number is at the start of the string: ("123 hello everybody 4").replace(/(^\d+)(.+$)/i,'$1'); //=> '123' If it's somewhere in the string: (" hello 123 everybody 4").replace( /(^.+)(\w\d+\w)(.+$)/i

Python add leading zeroes using str.format [duplicate]

末鹿安然 提交于 2019-12-17 17:27:05
问题 This question already has answers here : Best way to format integer as string with leading zeros? [duplicate] (10 answers) Closed 6 years ago . 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

Reuse a parameter in String.format?

对着背影说爱祢 提交于 2019-12-17 17:26:21
问题 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? 回答1: From the docs: The format specifiers for general, character, and numeric types have the following syntax: %[argument_index$][flags][width][.precision