This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 4 years ago.
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
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 \
"'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'"""
Related
This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 2 years ago.
I'm trying to match this:
text = "111111"
reps = 2
f_pattern = re.compile(rf"(\w)(?=\1{{reps}})")
f_matches = re.findall(f_pattern, text)
## returns: []
r_pattern = re.compile(r"(\w)(?=\1{2})")
r_matches = re.findall(r_pattern, text)
## returns: ['1', '1', '1', '1']
How should the f-string pattern be written to return non-empty result?
Write rf"(\w)(?=\1{{{reps}}})") instead of rf"(\w)(?=\1{{reps}})").
{{ is a way to escape single { inside any f-string, same for }.
Try it online!
As mentioned in this answer:
How do I use format() in re.compile
Double braces are interpreted as literal braces in format strings. You need another, third set, to indicate that len is a formatted expression.
If you use double brace, f-string thinks its a literal brace and skips it. use 3 braces so 2 of the braces will be considered as a literal brace and another set for format string.
You need to double the curly bracket for literal print. so here is the solution
text = "111111"
reps = 2
f_pattern = re.compile(rf"(\w)(?=\1{{ {reps} }})")
f_matches = re.findall(f_pattern, text)
This question already has answers here:
Python split string without splitting escaped character
(10 answers)
Closed 5 years ago.
Is there any better way to split a string which contains escaped delimeter in it.
string = "fir\&st_part&secon\&d_part"
print(string.split('&'))
# is giving me
>>> ['fir\\', 'st_part', 'secon\\', 'd_part']
# but not
>>> ['fir&st_part', 'secon&d_part']
I have added an escape character \ before & in fir&st_part and secon&d_part with the intention that split function will escape the following character.
Is there any better way to do this if not by using a string split?
You can user regular expression!
split if ?<! current position of string is not preceded with backward (\, two slashes to escape it)slash and ampersand symbol(&)
>>> import re
>>> re.split(r'(?<!\\)&', string)
['fir\\&st_part', 'secon\\&d_part']
With the resulting list, you can iterate and replace the escaped '\&' with '&' if necessary!
>>> import re
>>> print [each.replace("\&","&") for each in re.split(r'(?<!\\)&', string)]
['fir&st_part', 'secon&d_part']
It's possible using a regular expression:
import re
string = "fir\&st_part&secon\&d_part"
re.split(r'[^\\]&', string)
# ['fir\\&st_par', 'secon\\&d_part']
This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 7 months ago.
Why does:
B = "The" + "\s"
and
B = "The" + r"\s"
yield:
"The\\s"
Is it possible to write the above, such that the output string is:
"The\s"
I have read similar questions on both the issue of backslashes, and their property for escaping, and the interpretation of regex characters in Python.
How to print backslash with Python?
Why can't Python's raw string literals end with a single backslash?
Does this mean there is no way to write what I want?
If it is useful, My end goal is to a write a program that adds the regex expression for space (\s) to a string where this such space:
For example, start with:
A = "The Cat and Dog"
After applying the function, this becomes:
B = "The\sCat\sand\sDog"
I believe this is related to Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
The representation of the string and what it actually contains can differ.
Observe:
>>> B = "The" + "\s"
>>> B
'The\\s'
>>> print B
The\s
Furthermore
>>> A = "The Cat and Dog"
>>> B = str.replace(A, ' ', '\s')
>>> B
'The\\sCat\\sand\\sDog'
>>> print B
The\sCat\sand\sDog
From the docs:
all unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the result
So while \s is not a proper escape sequence, Python forgives you your mistake and treats the backslash as if you had properly escaped it as \\. But when you then view the string's representation, it shows the backslash properly escaped. That said, the string only contains one backslash. It's only the representation that shows it as an escape sequence with two.
You must escape the "\"
B = "The" + "\\s"
>>> B = "The" + "\\s"
>>> print(B)
The\s
See the Escape Sequences part:
Python 3 - Lexical Analysis
This question already has an answer here:
python re.split lookahead pattern
(1 answer)
Closed 6 years ago.
I`m trying to split and to rename some ugly looking variable names (as an example):
In[1]: import re
ugly_names = ['some-Ugly-Name', 'ugly:Case:Style', 'uglyNamedFunction']
new_names = []
In[2]: patt = re.compile(r'(?<=[a-z])[\-:]?(?=[A-Z])')
In[3]: for name in ugly_names:
loc_name = patt.split(name)
new_names.append("_".join(s.lower() for s in loc_name))
print(new_names)
Out[3]: ['some_ugly_name', 'ugly_case_style', 'uglynamedfunction']
What's wrong with my pattern? Why doesn't it match on empty string, or I'm missing something?
p.s.: Is it possible with Python's regex to split on empty strings or should I use some other functions and .groups()?
Not a direct answer to the question, but just an alternative way - use the inflection library (have to handle : separately though):
>>> import inflection
>>>
>>> [inflection.underscore(name.replace(":", "_")) for name in ugly_names]
['some_ugly_name', 'ugly_case_style', 'ugly_named_function']
This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 3 years ago.
Given a variable which holds a string is there a quick way to cast that into another raw string variable?
The following code should illustrate what I'm after:
line1 = "hurr..\n..durr"
line2 = r"hurr..\n..durr"
print(line1 == line2) # outputs False
print(("%r"%line1)[1:-1] == line2) # outputs True
The closest I have found so far is the %r formatting flag which seems to return a raw string albeit within single quote marks. Is there any easier way to do this kind of thing?
Python 3:
"hurr..\n..durr".encode('unicode-escape').decode()
Python 2:
"hurr..\n..durr".encode('string-escape')
Yet another way:
>>> s = "hurr..\n..durr"
>>> print repr(s).strip("'")
hurr..\n..durr
Above it was shown how to encode.
'hurr..\n..durr'.encode('string-escape')
This way will decode.
r'hurr..\n..durr'.decode('string-escape')
Ex.
In [12]: print 'hurr..\n..durr'.encode('string-escape')
hurr..\n..durr
In [13]: print r'hurr..\n..durr'.decode('string-escape')
hurr..
..durr
This allows one to "cast/trasform raw strings" in both directions. A practical case is when the json contains a raw string and I want to print it nicely.
{
"Description": "Some lengthy description.\nParagraph 2.\nParagraph 3.",
...
}
I would do something like this.
print json.dumps(json_dict, indent=4).decode('string-escape')