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.
Related
This question already has answers here:
Use Python Match object in string with backreferences
(1 answer)
Regex in python: is it possible to get the match, replacement, and final string?
(2 answers)
Closed 3 years ago.
What is an example usage of Match.expand ? It doesn't give any examples in the python docs (which is the first I've heard of the method), and only states:
Match.expand(template)
Return the string obtained by doing backslash substitution on the template string template, as done by the sub() method.
How would this actually be used and how could it be useful?
you can find more explanations and examples here:
match.expand
In general:
it allows you to expand the match you have found and modify its' prefix
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)
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.
This question already has answers here:
How to use a variable inside a regular expression?
(12 answers)
Closed 5 years ago.
I want to eventually be able to increment an integer within my Regex, but the braces are preventing me from doing so.
So far I have:
start = 6
m = re.search(r"(.{{n},}).*?\1".format(n=start), s)
return m.group(1)
However, I get `ValueError: Single '}' encountered in format string
I am using Python 2.7.
What about using a different method of string formatting:
m = re.search(r"({.%s,}).*?\1" % start, s)