string-formatting

Convert hex to binary

夙愿已清 提交于 2019-11-26 19:31:07
I have ABC123EFFF. I want to have 001010101111000001001000111110111111111111 (i.e. binary repr. with, say, 42 digits and leading zeroes). How? Onedinkenedi For solving the left-side trailing zero problem: my_hexdata = "1a" scale = 16 ## equals to hexadecimal num_of_bits = 8 bin(int(my_hexdata, scale))[2:].zfill(num_of_bits) It will give 00011010 instead of the trimmed version. import binascii binary_string = binascii.unhexlify(hex_string) Read binascii.unhexlify Return the binary data represented by the hexadecimal string specified as the parameter. bin(int("abc123efff", 16))[2:] >>> bin(

Better String formatting in Scala

本小妞迷上赌 提交于 2019-11-26 19:13:56
问题 With too many arguments, String.format easily gets too confusing. Is there a more powerful way to format a String. Like so: "This is #{number} string".format("number" -> 1) Or is this not possible because of type issues ( format would need to take a Map[String, Any], I assume; don’t know if this would make things worse). Or is the better way doing it like this: val number = 1 <plain>This is { number } string</plain> text even though it pollutes the name space? Edit: While a simple pimping

Formatting Phone number in Swift

六眼飞鱼酱① 提交于 2019-11-26 19:09:56
问题 I'm formatting my textfiled text once the user start typing the phone number into this format type 0 (555) 444 66 77 and it is working fine but once I get the number from the server I get it like this 05554446677 So please could you tell me how I can edit it in the same format once I get it fro the server? My code once I start typing: func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if textField ==

Using variables inside strings

烈酒焚心 提交于 2019-11-26 18:51:22
In PHP I can do the following: $name = 'John'; $var = "Hello {$name}"; // => Hello John Is there a similar language construct in C#? I know there is String.Format(); but I want to know if it can be done without calling a function/method on the string. In C# 6 you can use string interpolation : string name = "John"; string result = $"Hello {name}"; The syntax highlighting for this in Visual Studio makes it highly readable and all of the tokens are checked. This functionality is not built-in to C# 5 or below. Update: C# 6 now supports string interpolation, see newer answers. The recommended way

Slicing strings in str.format

ε祈祈猫儿з 提交于 2019-11-26 18:30:36
问题 I want to achieve the following with str.format : x,y = 1234,5678 print str(x)[2:] + str(y)[:2] The only way I was able to do it was: print '{0}{1}'.format(str(x)[2:],str(y)[:2]) Now, this an example and what I really have is a long and messy string, and so I want to put slicing inside the {} . I've studied the docs, but I can't figure out the correct syntax. My question is: is it possible to slice strings inside a replacement field? 回答1: No, you cannot apply slicing to strings inside a the

Format a datetime into a string with milliseconds

ぐ巨炮叔叔 提交于 2019-11-26 18:28:24
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 To get a date string with milliseconds (3 decimal places behind seconds), use this: from datetime import datetime print datetime.utcnow().strftime('%Y-%m-

Calculate a Ratio in C#

白昼怎懂夜的黑 提交于 2019-11-26 18:22:12
问题 I thought this would be simple, but searching Google didn't seem to help. I'm basically trying to write a function which will return a ratio as a string (eg 4:3) when supplies with two integers (eg 800 and 600). string GetRatio(Int A, Int B) { // Code I'm looking for return Ratio; } 回答1: You can simplify fractions by dividing numerator and denominator by their GCD: var gcd = GCD(A, B); return string.Format("{0}:{1}", A / gcd, B / gcd) And a very basic function for calculating the GCD, using

How to center a string using String.format?

不打扰是莪最后的温柔 提交于 2019-11-26 17:50:00
问题 public class Divers { public static void main(String args[]){ String format = "|%1$-10s|%2$-10s|%3$-20s|\n"; System.out.format(format, "FirstName", "Init.", "LastName"); System.out.format(format, "Real", "", "Gagnon"); System.out.format(format, "John", "D", "Doe"); String ex[] = { "John", "F.", "Kennedy" }; System.out.format(String.format(format, (Object[])ex)); } } output: |FirstName |Init. |LastName | |Real | |Gagnon | |John |D |Doe | |John |F. |Kennedy | I want the output to be centered.

How can I convert 24 hour time to 12 hour time?

假如想象 提交于 2019-11-26 17:49:23
I have the following 24-hour times: {'Wed': '10:30 - 21:00', 'Sun': '10:30 - 21:00', 'Thu': '10:30 - 21:00', 'Mon': '10:30 - 21:00', 'Fri': '10:30 - 22:00', 'Tue': '10:30 - 21:00', 'Sat': '10:30 - 22:00'} How can I convert this to 12-hour time? {'Wed': '10:30 AM - 09:00 PM', 'Sun': '10:30 AM - 09:00 PM', 'Thu': '10:30 AM - 09:00 PM', 'Mon': '10:30 AM - 09:00 PM', 'Fri': '10:30 AM- 10:00 PM', 'Tue': '10:30 AM- 09:00 PM', 'Sat': '10:30 AM - 11:00 PM'} I want to intelligently convert "10.30" to "10.30 AM" & "22:30" to "10:30 PM" . I can do using my own logic but is there a way to do this

Add custom conversion types for string formatting

半城伤御伤魂 提交于 2019-11-26 17:49:16
问题 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