Python - String Escape Sequences

Introduction

To embed a quote inside a string, precede it with a backslash.

Backslashes introduce special character codings known as escape sequences.

Escape sequences embed characters that cannot be typed on a keyboard.

The character \, and one or more characters following it in the string literal, are replaced with a single character in the resulting string.

The following code has a five-character string that embeds a newline and a tab:

s = 'a\nb\tc' 

The two characters \n stand for a single character-the binary value of the newline character in your character set.

The sequence \t is replaced with the tab character.

Demo

s = 'a\nb\tc' 
print( s )

Result

To get how many actual characters are in this string, use the built-in len function.

It returns the actual number of characters in a string, regardless of how it is coded or displayed:

Demo

s = 'a\nb\tc' 
print( len(s) )

Result

Related Topics