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?
#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


Messages In This Thread
RE: Is it possible to have an apostrophe inside { } in an f-string? - by ThomasL - Sep-04-2019, 12:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  CSV not printing apostrophe correctly bazcurtis 8 2,550 Aug-22-2022, 04:28 PM
Last Post: bazcurtis
  converting string object inside a list into an intiger bwdu 4 2,674 Mar-31-2020, 10:36 AM
Last Post: buran
  Python Parameter inside Json file treated as String dhiliptcs 0 1,866 Dec-10-2019, 07:28 PM
Last Post: dhiliptcs
  If statement containing apostrophe AshBax 11 5,312 Jul-09-2019, 01:34 PM
Last Post: AshBax
  How do I put a variable inside a String in Python? evilcode1 8 6,152 Sep-25-2018, 04:53 PM
Last Post: evilcode1
  replace string inside double quotes jmpatx 10 16,744 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