Python - convert format string to f-string [duplicate] - python

This question already has answers here:
How can I do a dictionary format with f-string in Python 3 .6?
(7 answers)
Closed 11 months ago.
How can I convert this to an f-string?
message = '{status} : {method} : {url}'.format(**call_components)

str.format() is usually fine, but if you really want an f-string then it would look like this:
message = f'{call_components["status"]} : {call_components["method"]} : {call_components["url"]}'

Related

How to write file locations as string in python [duplicate]

This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 2 years ago.
I cant seem to write something like : "C:\Users\alon4\Desktop\Learning Material\Odyssey\Lab a2\Magnetism\data"
As a string to print or otherwise, i think its something to do with the slashes
There can be 3 ways :
1) Either use r (raw strings) before the actual string :
For example : r"C:\Users\alon4\Desktop\Learning Material\Odyssey\Lab a2\Magnetism\data"
2) Or use // like:
For example : "C:\\Users\\alon4\\Desktop\\Learning Material\\Odyssey\\Lab a2\\Magnetism\data"
3) Use slashes: "C:/Users/alon4/Desktop/Learning Material/Odyssey/Lab a2/Magnetism/data"
(As suggested by Óscar López)

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

Python print ascii character in string instead of value [duplicate]

This question already has answers here:
Decode HTML entities in Python string?
(6 answers)
Closed 3 years ago.
I have a string "hello[ World]" and I want to convert it to "hello[World]"
I tried something like this:
a.encode("utf-8").decode("ascii")
I got back same string as input.
Try this:
import html
html.unescape("LASIX [FUROSEMIDE]")
This produces:
'LASIX [FUROSEMIDE]'

escaping {} brackets in triple quote Python strings [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 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}

Parsing a URL using regular expression [duplicate]

This question already has answers here:
What's the u prefix in a Python string?
(5 answers)
Closed 6 years ago.
I am trying to parse the 'Meghan' part from the line:
link = http://python-data.dr-chuck.net/known_by_Meghan.html
...with the following regex:
print re.findall('by_(\S+).html$',link)
I am getting the output:
[u'Meghan']
Why I am getting the 'u'?
It means unicode. Depending on what you'll do with it, you can ignore it for the most part, of you can convert it to ascii by doing .encode('ascii')

Categories