escaping {} brackets in triple quote Python strings [duplicate] - python

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}

Related

Trouble using python re with the format option [duplicate]

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 2 years ago.
I have multiple files of the format myfilexyz-200407171758.tar.gz
(myfilexyz)-(200407171758).tar.gz
Group1 is a variable.
Group2 can be of 12 to 14 digits.
Using variable substitution, I can get this working
r = re.compile('(%s)-(\d){12,13}.tar.gz' %myvar)
But if I were to try the newer format method, I get into trouble
r = re.compile('({})-(\d){12,14}.tar.gz'.format(myvar))
key '12,14' has no corresponding arguments
Obviously the {12,14} is messing up format. Is there a way around this problem and still use the format method for substitution?
From documentation,
If you need to include a bracing character in the literal text, it can be escaped by doubling:
{{ and }}.
Use
'({})-(\d){{12,14}}.tar.gz'.format(myvar)
Also, format is older way of doing it. Use f-string
f'({myvar})-(\d){{12,14}}.tar.gz'
Why not concatenate directly?
myvar + '-(\d){{12,14}}.tar.gz'

Put single and double quote in Python string format? [duplicate]

This question already has answers here:
Python string literals - including single quote as well as double quotes in string
(3 answers)
Using quotation marks inside quotation marks
(12 answers)
Closed 3 years ago.
I'm having trouble figuring out how to print a very specific line.
I tried using .format and I also tried the print("str",variable,"str") method but I can't seem to figure out how to make it print correctly.
feet=5
inches=6
print('Room Length:{}' {}"'.format(feet,inches))
I want the computer to print; Room Length: 5' 6"
but I am not sure how to print this while keeping the apostrophe and the quotations for feet and inches.
print('Room Length:{}\' {}\"'.format(feet,inches))

how can I solve keyerror when formatting string in pythong [duplicate]

This question already has answers here:
Python format throws KeyError
(1 answer)
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 3 years ago.
I just want the following outcome.
But I get KeyError: '"msg_body"'.
input:
text="text"
uid="uid"
input = '{"msg_body":{input_text}, "user_id":{input_uid}}'.format(input_text=text, input_uid=test)
wanted output:
'{"msg_body":"text", "user_id":"uid"}'
Single or double quotations must be exactly how it is above.
Thanks

How to programmatically convert r'\n' to '\n' in python? [duplicate]

This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 7 months ago.
Let's say I have a raw string like r'\n' that corresponds to '\\n'. Is there a way to interpret the raw string as the "normal" string '\n' with the line break meaning?
You can get a newline by r'\n'.replace("\\n", "\n")

%% in sec2time() Python [duplicate]

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.

Categories