1

How can i do to represent a string with (\") inside string

I tried several ways:

date = 'xpto\"xpto'
'xpto"xpto'
date = 'xpto\\"xpto'
'xpto\\"xpto'
data='xpto\\' + '"xpto'
'xpto\\"xpto'
data= r'xpto\"xpto'
'xpto\\"xpto'

i need the string exactly like this

'xpto\"xpto'

if someone knows how, I really appreciate the help

rnnhm
  • 53
  • 6
  • `'xpto\\"xpto'` works just fine for me... – Joshua Yonathan Apr 25 '18 at 20:24
  • @Aran-Fey I would not agree with you on this one, it is a specific use-case and even tho his question falls into the same category it does require triple escaping and a brief explanation on how it actually works. – – innicoder Apr 25 '18 at 20:35
  • @ElvirMuslic I think it's enough to clear up the OP's misunderstanding. Once they know _why_ the strings are printed with double backslashes, it's easy to find out which of these 4 options produce the expected results. No fancy escaping is required here. It's easy enough to figure out and I don't see the benefit of having an answer that shows the OP how to build that exact string. Nobody will ever come across this question and think *"Hey, that's the exact same string I wanted to build! Good thing I can just copy/paste it from here!"*. If it's really necessary, it can be done in the comments. – Aran-Fey Apr 25 '18 at 20:42
  • @Aran-Fey I do agree that if it were a question with a wider-lens i.e. `how does this apply to all strings?` not just to this one it would've been more helpful. Much more helpful and the fact that people might face the same issue makes it deserving of an answer. Thank you for your great contributions and many, many helpful answer and comments I do appreciate it, having that I stick to what I said. I just have an issue with the Base-Style answer, you can always (in a sense) refer to a basic type of answer that someone has written. – innicoder Apr 25 '18 at 20:48
  • @ElvirMuslic I think it's more important to provide a good and detailed explanation of the problem than it is to hold the OP's hand and spoonfeed them a solution. You mustn't forget that many people come to StackOverflow from google, and a proper explanation is ___much___ more useful for them than a copy/paste-able string for the OP. None of the answers down there explain the underlying problem nearly as well as the duplicate I linked does. In the long run, marking this as a dupe will help more people. If you disagree, feel free to cast a reopen vote. But my vote stays, at least for now. – Aran-Fey Apr 25 '18 at 20:58
  • @Aran-Fey I understand your concern, understanding the underlying problem is fine and I agree in some cases, people do come from Google and that's the issue, they're looking for a solution to their problem. I don't need to understand advanced algebra (mostly) to perform math calculations onto my datasets. Respectfully there is a fine line there and spoon feeding is necessary especially with simple questions as you want to encourage people to grow. Too much knowledge too much detail = burden. That is all I'm saying. We the people of the internet want to make it easy. Thank you for your time – innicoder Apr 25 '18 at 21:11

2 Answers2

1

The following line works.

print(r"'xpto\"xpto'")

Output:

'xpto\"xpto'

We add r to insinuate that the string is in a raw format.

and/or

print("'xpto\\\"xpto'") where \\ = \ escapes this and \" = " escaping the " with \

innicoder
  • 2,612
  • 3
  • 14
  • 29
0

"'xpto\\\"xpto'" is correct. Part of the confusion is distinguishing the actual string with Python's textual representation of the string.

>>> date = "'xpto\\\"xpto'"
>>> date
'\'xpto\\"xpto\''
>>> print(date)
'xpto\"xpto'

A simpler solution (which came to mind after reading Elvir's answer) is to use a triple-quoted raw string:

date = r"""'xpto\"xpto'"""
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Awesome! I first typed the triple quotes and later removed them for simplicity reasons, however, it's really useful `''''''` or `""""""` The string will have to be ended by triple either single or double quote and thereby (almost) eliminating the chance of an issue arising with the quotes inside the string. – innicoder Apr 25 '18 at 20:34