This question already has answers here:
How can I selectively escape percent (%) in Python strings?
(6 answers)
What is %% for in Python? [duplicate]
(1 answer)
Closed 4 years ago.
I came across a problem when I was learning python.
print('test%d, %.2f%%' % (1,1.4))
however, it has an error.
ValueError: incomplete format
But if I execute like this:
print('test%d, %.2f%%' % (1,1.4))
test1, 1.40%
It works and prints the '%'. But I don't know why? Can someone help me? Thanks.
Since % is used as a special character in (old C-style) format strings, you have to use %% to print a literal percent sign.
You need to look into c-style string formatting. %% is a reference to this series of string formatting commands.
The following page:
https://docs.python.org/3.4/library/string.html
has a "String formatting mini-language" section that answers your question in meticulous detail.
Related
This question already has answers here:
In Python format (f-string) strings, what does !r mean? [duplicate]
(1 answer)
How to left align a fixed width string?
(8 answers)
Closed 1 year ago.
I was following a tutorial and found this.
print(f"{self.name}: {card!r:<3} ", end="")
And I have no idea what the !r:<3 means. Google wouldn't give me relevant results because of all the symbols.
You can find a description of the format of f-strings here and of the formatting language here.
!r:<3 uses repr to format the value, left aligned with a minimum width of 3 (padded with spaces by default).
This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 3 years ago.
how can you format a string of this form in Python 3?
'''{name}{{name}}'''.format(name="bob")
the desired output is: bob{bob}, but the above gives: bob{name}.
one solution is to add another argument to format:
'''{name1}{name2}'''.format(name1="bob", name2="{bob}")
but this is excessive. is there a way to properly escape { such that inner {x} can still be interpolated and one can only pass a single name to format?
Add one more level of {}:
'''{name}{{{name}}}'''.format(name="bob")
which outputs:
bob{bob}
This question already has answers here:
What does %s mean in a Python format string?
(7 answers)
String formatting in Python [duplicate]
(14 answers)
String formatting: % vs. .format vs. f-string literal
(16 answers)
Closed 4 years ago.
I'm learning multithreading in python and I was reading through this answer. I understand most of the code however there is this one line which I simply don't understand and I don't know how to search for it on Google as the '%' sign keeps returning modulo.
req.headers['Range'] = 'bytes=%s-%s' % (start, start+chunk_size)
I thought that req.headers['Range'] would retrieve some 'range' element from an array however here they are assigning it a value of 'bytes=%s-%s' % (start, start+chunk_size). I really just don't understand what is going on in this line. Things like 'bytes=%s-%s' I am assuming is some sort of python syntax which I am unaware of. If you could explain each term in this line that would be very much appreciated.
In python there are multiple ways to format strings. using %s inside a string and then a % after the string followed by a tuple (or a single value), allows you to create a new string:
x = 5
y = 8
'my favourite number is %s, but I hate the number %s' % (x, y)
results in:
'my favourite number is 5, but I hate the number 8'
I think they call it C-type string formatting. For more information, you can check out this page.
In my opinion, it is easier to format string using f'strings, or .format(). Check out this page too
This question already has answers here:
How can I selectively escape percent (%) in Python strings?
(6 answers)
Closed 6 years ago.
In the sec2time() Python function provided by Lee he uses a syntax I'm struggling to understand:
pattern = '%%02d:%%02d:%%0%d.%df' % (n_msec+3, n_msec)
What is the %%here and how does it affect the outcome?
The % in that string introduces replaceable parts as at the end %d.%df. If you want a % in the output you have to do something special, in this case use %%
After these substitustions the resulting pattern will look like:
'%02d:%02d:%0123.120f'
which, among other things an be used for further substitution.
In the documentation, at the bottem of the second table in that section, it states:
'%' No argument is converted, results in a '%' character in the result.
This question already has answers here:
Add 'decimal-mark' thousands separators to a number
(9 answers)
Closed 8 years ago.
I am simply trying to execute the following command in Python:
print("Number is", format(49000.12345,"10.2f"))
So I would like it to print out like 49,000.12 for the number.
My teacher taught us to put a comma in "10.2f" like ",10.2f" for the thousands separator but it's not working. Could someone please tell me the correct simple way similar to that?
Thank you
See this: http://www.python.org/dev/peps/pep-0378/ It is the PEP introducing the ability into Python 2.7, 3.1 and later versions.