How to escape escape-characters

2024/11/15 11:42:39

I have a string variable which is not printing properly, I guess because it contains escape characters. How to escape these escape-characters?

>>> print "[p{Aa}\\P{InBasic_Latin}\r\t\n]"
>>> ]

such that it prints this [p{Aa}\\P{InBasic_Latin}\r\t\n] instead of what's currently printing ]

Answer

To solve your problem you will need to use Backslash characters. Backslash ( \ ) characters are used to escape characters

In your question \, \r, \t, \n are the characters you want to escape from your print statement.

Answer is : print "[p{Aa}\\\P{InBasic_Latin}\\r\\t\\n]"

The use of backslash is it will escape the very first character after it.

In the case where you want to print "\P", here you need to add one slash at the start "\" so that it escapes the first backslash then afterward it will print \P. As P is not a special character you don't need to add one more backslash to escape it.

https://en.xdnf.cn/q/119665.html

Related Q&A

Python Multiprocessing a large dataframe on Linux

As shown in the title, I have a big data frame (df) that needs to be processed row-wise, as df is big (6 GB), I want to utilize the multiprocessing package of python to speed it up, below is a toy exam…

How to pass more arguments through tkinter bind

How do I pass more arguments through tkinters bind method? for the example:tk = Tk() def moveShip(event,key):if event.keysym == Down and player1.selectedCoord[1] != 9:if key == place:player1.selectedC…

Python Class method definition : unexpected indent [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 6 months ago.I am getting started with Django and Python so naturally Im doing …

pandas python - round() not behaving correctly

Im rounding values in a dataframe to 1 decimal place. Here is the dfVren 2015 Hsten 2014 Vren 2014 Question 1) Maten r vllagad oc…

Converting dictionary into string

d={a:Apple,b:ball,c:cat}The above dictionary I have and I want my Output like the below-mentioned resultres="a=Apple,b=ball,c=cat"Is it possible in a pythonic way then please answer it I have…

Django Getting QuerySet.values() based on condition

Lets say I have a model Class Parent and a Class Child. And child has a field called status and a ForeignKey relationship to Parent.Lets say I retrieve one parent by calling filter (so as to have a Que…

Condition checking in python with user input [duplicate]

This question already has answers here:If...else statement issue with raw_input on Python(2 answers)Closed 8 years ago.I tried taking an input from keyboard. the checking that input with an if else sta…

Triangle of T in Python

EDIT ** I cant multiply strings by an integer. Its for a homework and those were the instructions **I need to do a triangle in python using for loops or while loops(mandatory). The final output should …

Finding prime project euler

Find the 10,001st prime number.I am trying to do Project Euler problems without using copying and pasting code I dont understand. I wrote code that finds whether a number is prime or not and am trying …

Splitting a python string

I have a string in python that I want to split in a very particular manner. I want to split it into a list containing each separate word, except for the case when a group of words are bordered by a par…