string-formatting

How can I fill out a Python string with spaces?

时光怂恿深爱的人放手 提交于 2019-11-25 23:16:12
问题 I want to fill out a string with spaces. I know that the following works for zero\'s: >>> print \"\'%06d\'\"%4 \'000004\' But what should I do when I want this?: \'hi \' of course I can measure string length and do str+\" \"*leftover , but I\'d like the shortest way. 回答1: You can do this with str.ljust(width[, fillchar]): Return the string left justified in a string of length width . Padding is done using the specified fillchar (default is a space). The original string is returned if width is

Display number with leading zeros

南楼画角 提交于 2019-11-25 22:38:51
问题 Given: a = 1 b = 10 c = 100 How do I display a leading zero for all numbers with less than two digits? That is, 01 10 100 回答1: In Python 2 you can do: print "%02d" % (1,) Basically % is like printf or sprintf . For Python 3.+ the same behavior can be achieved with: print("{:02d}".format(1)) For Python 3.6+ the same behavior can be achieved with f-strings: print(f"{1:02d}") 回答2: You can use str.zfill: print str(1).zfill(2) print str(10).zfill(2) print str(100).zfill(2) prints: 01 10 100 回答3:

How can I print literal curly-brace characters in python string and also use .format on it?

无人久伴 提交于 2019-11-25 21:47:07
问题 x = \" \\{ Hello \\} {0} \" print x.format(42) gives me : Key Error: Hello\\\\ I want to print the output: {Hello} 42 回答1: 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 considered literal text, which is copied unchanged to the output. If you

String formatting: % vs. .format

丶灬走出姿态 提交于 2019-11-25 21:39:01
问题 Python 2.6 introduced the str.format() method with a slightly different syntax from the existing % operator. Which is better and for what situations? The following uses each method and has the same outcome, so what is the difference? #!/usr/bin/python sub1 = \"python string!\" sub2 = \"an arg\" a = \"i am a %s\" % sub1 b = \"i am a {0}\".format(sub1) c = \"with %(kwarg)s!\" % {\'kwarg\':sub2} d = \"with {kwarg}!\".format(kwarg=sub2) print a # \"i am a python string!\" print b # \"i am a

Named string formatting in C#

狂风中的少年 提交于 2019-11-25 20:26:02
Is there any way to format a string by name rather than position in C#? In python, I can do something like this example (shamelessly stolen from here ): >>> print '%(language)s has %(#)03d quote types.' % \ {'language': "Python", "#": 2} Python has 002 quote types. Is there any way to do this in C#? Say for instance: String.Format("{some_variable}: {some_other_variable}", ...); Being able to do this using a variable name would be nice, but a dictionary is acceptable too. John Sheehan There is no built-in method for handling this. Here's one method string myString = "{foo} is {bar} and {yadi}

Python's many ways of string formatting — are the older ones (going to be) deprecated?

血红的双手。 提交于 2019-11-25 19:13:15
Python has at least six ways of formatting a string: In [1]: world = "Earth" # method 1a In [2]: "Hello, %s" % world Out[2]: 'Hello, Earth' # method 1b In [3]: "Hello, %(planet)s" % {"planet": world} Out[3]: 'Hello, Earth' # method 2a In [4]: "Hello, {0}".format(world) Out[4]: 'Hello, Earth' # method 2b In [5]: "Hello, {planet}".format(planet=world) Out[5]: 'Hello, Earth' # method 2c In [6]: f"Hello, {world}" Out[6]: 'Hello, Earth' In [7]: from string import Template # method 3 In [8]: Template("Hello, $planet").substitute(planet=world) Out[8]: 'Hello, Earth' A brief history of the different