Python Forum
get string how it is defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get string how it is defined
#1
I can have any characters inside the string. For eg.

str = "some\" quoted value'foo"
print(str)
# some" quoted value'foo
How can I get it printed intact?
# some\" quoted value'foo
Reply
#2
The point is that there are several ways to represent literally a string. The general idea is to use repr()
>>> s = "some \" quo\u0074ed value'foo"
>>> print(repr(s))
'some " quoted value\'foo'
You cannot be 100% sure to recover the initial form because the compiler interpretes the string and it doesn't remember that the " was initially escaped and ' was not for example, or that t was written \u0074
Reply
#3
Ah, that doesn't help me. What I'm trying to do is combine that string in previous string. For eg.

str = "{a:"
str += "some \" quoted value'foo"
str += "}"
Which should result in:

# {a:"some \" quoted value'foo"}
# But not:
# {a:"some " quoted value\'foo"}
So, what I want is what user has inputed exactly.
Reply
#4
Never use str as name. Otherwise you can find yourself wondering:

In [1]: str(100)                                                                
Out[1]: '100'

In [2]: str = "some\" quoted value'foo"                                         

In [3]: str(100)                                                                
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-1aa51eeeed3a> in <module>
----> 1 str(100)

TypeError: 'str' object is not callable
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
You need to understand that when you write
s = "some \" quoted value 'foo"
the string s doesn't contain any \ character. It means that your requirement that "it should result in" a string with a backslash character is not well defined. You'll have to write your own escaping function and implement your own rules, for example you could replace all the " in a string with \".
Reply
#6
I may not know what user may post in their inputs. So, it's hard know what to replace. Or, is it just fine to replace all backslashes?
Reply
#7
your post is example of classical XY problem in my view. it looks like you try to construct json string/file and doing so the wrong way.
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
#8
As Griboullis explained there are subtle differences how string stored and how it's printed out:

>>> backslash = '\\'
>>> a = "{a:"
>>> a += f"some {backslash}\" quoted value'foo"
>>> a += "}"
>>> print(a)
{a:some \" quoted value'foo}
>>> a
'{a:some \\" quoted value\'foo}'
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
#9
import json
foo = "some\" quoted value'foo"
spam = {'a':foo}
json_str = json.dumps(spam)
print(json_str)
Output:
{"a": "some\" quoted value'foo"}
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
#10
Ah, this works just fine! Thanks. Was forgetting about dumps. How can I mark your answer as solution?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 3,829 May-30-2020, 04:14 AM
Last Post: DT2000
  string name is not defined listingpython 2 2,618 Jan-17-2019, 09:44 AM
Last Post: listingpython

Forum Jump:

User Panel Messages

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