Python Forum
get string how it is defined - 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: get string how it is defined (/thread-17914.html)

Pages: 1 2


get string how it is defined - bhojendra - Apr-29-2019

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



RE: get string how it is defined - Gribouillis - Apr-29-2019

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


RE: get string how it is defined - bhojendra - Apr-29-2019

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.


RE: get string how it is defined - perfringo - Apr-29-2019

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



RE: get string how it is defined - Gribouillis - Apr-29-2019

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 \".


RE: get string how it is defined - bhojendra - Apr-29-2019

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?


RE: get string how it is defined - buran - Apr-29-2019

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.


RE: get string how it is defined - perfringo - Apr-29-2019

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}'



RE: get string how it is defined - buran - Apr-29-2019

import json
foo = "some\" quoted value'foo"
spam = {'a':foo}
json_str = json.dumps(spam)
print(json_str)
Output:
{"a": "some\" quoted value'foo"}



RE: get string how it is defined - bhojendra - Apr-29-2019

Ah, this works just fine! Thanks. Was forgetting about dumps. How can I mark your answer as solution?