Python Forum
Is it possible to have an apostrophe inside { } in an f-string?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible to have an apostrophe inside { } in an f-string?
#1
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 \?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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"
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
and string is a module from Python Standard Library and should not be used as variable name
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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")
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  CSV not printing apostrophe correctly bazcurtis 8 2,379 Aug-22-2022, 04:28 PM
Last Post: bazcurtis
  converting string object inside a list into an intiger bwdu 4 2,553 Mar-31-2020, 10:36 AM
Last Post: buran
  Python Parameter inside Json file treated as String dhiliptcs 0 1,817 Dec-10-2019, 07:28 PM
Last Post: dhiliptcs
  If statement containing apostrophe AshBax 11 5,111 Jul-09-2019, 01:34 PM
Last Post: AshBax
  How do I put a variable inside a String in Python? evilcode1 8 6,025 Sep-25-2018, 04:53 PM
Last Post: evilcode1
  replace string inside double quotes jmpatx 10 16,416 Apr-26-2017, 05:27 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020