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'
Related
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 1 year ago.
I am trying to split a string using regex on $ symbol but the output is not what I want.
string = "43$hello"
list_of_splits = re.split("$",string)
Output:
['43$hello','']
Output I want:
['43','hello']
It's visible by the output that "$" is a special character in regex, but now by how can I do this?
Use the escape character \ : list_of_splits = re.split("\$", str)
You can just use string split method.
string = "43$hello"
string.split("$")
Output
['43', 'hello']
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:
Why do backslashes appear twice?
(2 answers)
Closed 7 months ago.
Having some trouble including a single pair of backslashes in the result of a formatted string in Python 3.6. Notice that #1 and #2 produce the same unwanted result, but #3 results in too many backslashes, another unwanted result.
1
t = "arst '{}' arst"
t.format(d)
>> "arst '2017-34-12' arst"
2
t = "arst \'{}\' arst"
t.format(d)
>> "arst '2017-34-12' arst"
3
t = "arst \\'{}\\' arst"
t.format(d)
>> "arst \\'2017-34-12\\' arst"
I'm looking for a final result that looks like this:
>> "arst \'2017-34-12\' arst"
Your third example is correct. You can print it to make sure of that.
>>> print(t.format(d))
arst \'2017-34-12\' arst
What you are seeing in your console is in fact the representation of the string. You can indeed obtain it by using repr.
print(repr(t.format(d)))
"arst \\'2017-34-12\\' arst"
# ^------------^---Those are not actually there
A backlash is used to escape a special character. So in a string literal, a backlash must itself be escaped like so.
"This is a single backlash: \\"
Although if you want your string to be exactly as typed, use an r-string.
r"arst \'{}\' arst"
Put a 'r' in front of your string to declare it as a string literal
t = r"arst \'{}\' arst"
You are being mislead by the output. See: Quoting backslashes in Python string literals
In [8]: t = "arst \\'{}\\' arst"
In [9]: t
Out[9]: "arst \\'{}\\' arst"
In [10]: print(t)
arst \'{}\' arst
In [11]: print(t.format('21-1-2'))
arst \'21-1-2\' arst
In [12]:
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:
Process escape sequences in a string in Python
(8 answers)
Closed 9 years ago.
I have a string that looks like this:
>>> st = 'aaaaa\x12bbbbb'
I can convert it to a raw string via:
>>> escaped_st = st.encode('string-escape')
'aaaaa\\x12bbbbb'
How can I convert the escaped string back to the original string? I was trying to do something like this:
escaped_st.replace('\\\\', '\\')
Decode the encoded string with the same encoding:
>>> st = 'aaaaa\x12bbbbb'
>>> escaped_st = st.encode('string-escape')
>>> escaped_st
'aaaaa\\x12bbbbb'
>>> escaped_st.decode('string-escape')
'aaaaa\x12bbbbb'