This question already has answers here:
Understanding repr( ) function in Python
(5 answers)
Why do backslashes appear twice?
(2 answers)
Closed 7 years ago.
Why does >>> 'c\\\h' produces 'c\\\\h' via the python CLI
But >>> print 'c\\\h' produces c\\h
Python interpreter running in REPL mode prints representation (repr builtin) of result of last statement (it it exists and not a None):
>>> 5 + 6
11
For str objects representation is a string literal in a same form it is written in your code (except for the quotes that may differ), so it includes escape sequences:
>>> '\n\t1'
'\n\t1'
>>> print repr('\n\t1')
'\n\t1'
print statement (or function) on the other hand prints pretty string-conversion (str builtin) of an element, which makes all escape sequences being converted to actual characters:
>>> print '\n\t1'
<---- newline
1 <---- tab + 1
Related
This question already has answers here:
What is the difference between `>>> some_object` and `>>> print some_object` in the Python interpreter?
(5 answers)
What is the difference between __str__ and __repr__?
(28 answers)
Closed 3 years ago.
>>> str4 = """This too
... is a multiline one
... built with triple double-quotes."""
>>> str4 #A
'This too\nis a multiline one\nbuilt with triple double-quotes.'
>>> print(str4) #B
This too
is a multiline one
built with triple double-quotes.
In #A and #B , we print str4 , first implicitly, then explicitly using the print function.
why the output are different.
This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 7 months ago.
I want to create a raw unicode character from a string hex representation. That is, I have a string s = '\u0222' which will be the 'Ȣ' character.
Now, this works if I do
>>> s = '\u0222'
>>> print(s)
'Ȣ'
but, if I try to do concatenation, it comes out as
>>> h = '0222'
>>> s = r'\u' + '0222'
>>> print(s)
\u0222
>>> s
'\\u0222'
because as it can be seen, what's actually in string is '\\u' not '\u'. How can I create the unicode character from hex strings or, how can I enter a true single backslash?
This was a lot harder to solve than I initially expected:
code = '0222'
uni_code = r'\u' + code
s = uni_code.encode().decode('unicode_escape')
print(s)
Or
code = b'0222'
uni_code = b'\u' + code
s = uni_code.decode('unicode_escape')
print(s)
Entering \u0222 is only for string constants and the Python interpreter generates a single Unicode code point for that syntax. It's not meant to be constructed manually. The chr() function is used to generate Unicode code points. The following works for strings or integers:
>>> chr(int('0222',16)) # convert string to int base 16
'Ȣ'
>>> chr(0x222) # or just pass an integer.
'Ȣ'
And FYI ord() is the complementary function:
>>> hex(ord('Ȣ'))
'0x222'
This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 6 years ago.
I would need to stripoff "domain\" from "domain\name" to extract name which can be any name or the word name literally
>>> s="domain\name"
>>> x=s[5:]
>>> print(x)
n
ame
>>> s="domain\bh16"
>>> x=s[5:]
>>> print(x)
h16
>>> x=s[4:]
>>> print(x)
ih16
You can convert it to a raw string and use replace as normal
s = r"domain\bh16"
print(s.replace("domain\\", '')) #bh16
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 8 years ago.
for this code in python,
>>>word="spaces"
>>>for i in range(len(word)):
... print word[i],
...
s p a c e s
>>>
If I do not want any spaces in between the letters that are printed, what should I do ? In C for eg, the printing is by default next to the last printed character.
Use sys.stdout.write:
>>> word = 'spaces'
>>> for c in word:
... sys.stdout.write(c)
...
spaces>>>
Side note: As you can see above, you don't need to use indexes; Just iterate the string to get characters.
This question already has answers here:
Python 3 Special characters escaping
(2 answers)
Closed 9 years ago.
x='\r\n\t\t\t\t'
print(x)
The above code isn't working cos maybe of it is not recognising the special characters. So you could please help?
Use a raw string:
>>> x = r'\r\n\t\t\t\t'
>>> print(x)
\r\n\t\t\t\t
Escaping is tedious:
>>> x = '\\r\\n\\t\\t\\t\\t'
>>> print(x)
\r\n\t\t\t\t
To escape an escape sequence, you should first escape the backslash \ character. So you need to add double backslash \\ like this:
>> x = '\\r\\n\\t\\t\\t\\t'
>> print x
'\r\n\t\t\t\t'
Or you can print the raw format of the string which dumps the raw string as mentioned by jamylak
>> x = r'\r\n\t\t\t\t'
>> print '\r\n\t\t\t\t'
'\r\n\t\t\t\t'