Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Escape Characters

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Escape Characters

      In Python, a string becomes a raw string if it is prefixed with "r" or "R" before the quotation symbols. Hence 'Hello' is a normal string whereas r'Hello' is a raw string.
      >>> normal="Hello"
      >>> print (normal)
      Hello
      >>> raw=r"Hello"
      >>> print (raw)
      Hello
      
      In normal circumstances, there is no difference between the two. However, when the escape character is embedded in the string, the normal string actually interprets the escape sequence, whereas the raw string doesn't process the escape character.
      >>> normal="Hello\nWorld"
      >>> print (normal)
      Hello
      World
      >>> raw=r"Hello\nWorld"
      >>> print (raw)
      Hello\nWorld
      
      In the above example, when a normal string is printed the escape character '\n' is processed to introduce a newline. However, because of the raw string operator 'r' the effect of escape character is not translated as per its meaning.
      The newline character \n is one of the escape sequences identified by Python. Escape sequence invokes an alternative implementation character subsequence to "\". In Python, "\" is used as escape character. Following table shows list of escape sequences.
      Unless an 'r' or 'R' prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are
      Sr.No
      Escape Sequence & Meaning
      1
      \<newline>
      Backslash and newline ignored
      2
      \\
      Backslash (\)
      3
      \'
      Single quote (')
      4
      \"
      Double quote (")
      5
      \a
      ASCII Bell (BEL)
      6
      \b
      ASCII Backspace (BS)
      7
      \f
      ASCII Formfeed (FF)
      8
      \n
      ASCII Linefeed (LF)
      9
      \r
      ASCII Carriage Return (CR)
      10
      \t
      ASCII Horizontal Tab (TAB)
      11
      \v
      ASCII Vertical Tab (VT)
      12
      \ooo
      Character with octal value ooo
      13
      \xhh
      Character with hex value hh

      Example

      The following code shows the usage of escape sequences listed in the above table
      # ignore \
      s = 'This string will not include \
      backslashes or newline characters.'
      print (s)
      
      # escape backslash
      s=s = 'The \\character is called backslash'
      print (s)
      
      # escape single quote
      s='Hello \'Python\''
      print (s)
      
      # escape double quote
      s="Hello \"Python\""
      print (s)
      
      # escape \b to generate ASCII backspace
      s='Hel\blo'
      print (s)
      
      # ASCII Bell character
      s='Hello\a'
      print (s)
      
      # newline
      s='Hello\nPython'
      print (s)
      
      # Horizontal tab
      s='Hello\tPython'
      print (s)
      
      # form feed
      s= "hello\fworld"
      print (s)
      
      # Octal notation
      s="\101"
      print(s)
      
      # Hexadecimal notation
      s="\x41"
      print (s)
      
      It will produce the following output
      This string will not include backslashes or newline characters.
      The \character is called backslash
      Hello 'Python'
      Hello "Python"
      Helo
      Hello
      Hello
      Python
      Hello Python
      hello
      world
      A
      A

      Practice with Online Editor

      Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
      Remember to save code(Ctrl + S Or Command + S) before run it.