Python Forum
Is it possible to have an apostrophe inside { } in an f-string? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Is it possible to have an apostrophe inside { } in an f-string? (/thread-20869.html)



Is it possible to have an apostrophe inside { } in an f-string? - Exsul - Sep-04-2019

from random import choice as rc

string = f"never say you {rc(['can', 'can\'t'])} do something"

print(string)
Output:
File "C:/Users/Exsul/.PyCharmCE2019.2/config/scratches/scratch.py", line 3 string = f"never say you {rc(['can', 'can\'t'])}" ^ SyntaxError: f-string expression part cannot include a backslash Process finished with exit code 1
How can I put an apostrophe inside the curly brackets, since I can't escape an end quotation mark with \?


RE: Is it possible to have an apostrophe inside { } in an f-string? - ichabod801 - Sep-04-2019

I got it to work by making the outer string a triple quoted string. But really, at this point I'd use a format call or another variable to hold the result of the random.choice call.


RE: Is it possible to have an apostrophe inside { } in an f-string? - boring_accountant - Sep-04-2019

You can use chr() to convert the ASCII code 39 to an apostrophe:
string = f"never say you {rc(['can', 'can' + chr(39) + 't'])} do something"



RE: Is it possible to have an apostrophe inside { } in an f-string? - perfringo - Sep-04-2019

Just a gentle nudging: it's not good practice to obscure code with cryptic abbreviations.

If code base grows and other people read the code they will need additional effort to mentally parse what the heck 'rc' does. There are conventions of using abbreviations ('import pandas as pd', 'import numpy as np') but I don't think that 'from random import choice as rc' is widespread convention.


RE: Is it possible to have an apostrophe inside { } in an f-string? - buran - Sep-04-2019

and string is a module from Python Standard Library and should not be used as variable name


RE: Is it possible to have an apostrophe inside { } in an f-string? - ThomasL - Sep-04-2019

Error:
f-string expression part cannot include a backslash
Why not just read the error message properly and code accordingly?
And of course PEP8 and other coding standards are your friend.
from random import choice

can_or_cant = choice(['can', 'can\'t'])
sentence = f"never say you {can_or_cant} do something"
print(sentence)
Edit: OP question must be answered Yes, as this works without problems.
print(f"never say you can\'t do something")



RE: Is it possible to have an apostrophe inside { } in an f-string? - Exsul - Sep-05-2019

(Sep-04-2019, 05:53 AM)perfringo Wrote: Just a gentle nudging: it's not good practice to obscure code with cryptic abbreviations.

If code base grows and other people read the code they will need additional effort to mentally parse what the heck 'rc' does. There are conventions of using abbreviations ('import pandas as pd', 'import numpy as np') but I don't think that 'from random import choice as rc' is widespread convention.

Oh, I would never do that on code I intended other people to see. This is a personal project.