Reader small image

You're reading from  Scientific Computing with Python - Second Edition

Product typeBook
Published inJul 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781838822323
Edition2nd Edition
Languages
Right arrow
Authors (3):
Claus Führer
Claus Führer
author image
Claus Führer

Claus Führer is a professor of scientific computations at Lund University, Sweden. He has an extensive teaching record that includes intensive programming courses in numerical analysis and engineering mathematics across various levels in many different countries and teaching environments. Claus also develops numerical software in research collaboration with industry and received Lund University's Faculty of Engineering Best Teacher Award in 2016.
Read more about Claus Führer

View More author details
Right arrow

2.4.1 Escape sequences and raw strings

The string '\n' is used to insert a line break and '\t' inserts a horizontal tabulator (TAB) into the string to align several lines:

print('Temperature\t20\tC\nPressure\t5\tPa')

These strings are examples of escape sequences. Escape sequences always start with a backslash, \. A multiline string automatically includes escape sequences:

a=""" 
A multi-line
example"""
a # returns '\nA multi-line \nexample'

A special escape sequence is "\\", which represents the backslash symbol in text:

latexfontsize="\\tiny"
print(latexfontsize) # prints \tiny

The same can be achieved by using a raw string instead:

latexfs=r"\tiny" # returns "\tiny"
latexfontsize == latexfs # returns True

Note that in raw strings, the backslash remains in the string and is used to escape some special characters:

print(r"\"") # returns \"
print(r"\\") # returns \
print(r"\") # returns an error (why?)

A raw string is a convenient tool to construct strings in a readable manner. The result is the same:

r"\"" == '\\"'
r"She: \"I am my dad's girl\"" == 'She: \\"I am my dad\'s girl\\"'
Previous PageNext Page
You have been reading a chapter from
Scientific Computing with Python - Second Edition
Published in: Jul 2021Publisher: PacktISBN-13: 9781838822323
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Claus Führer

Claus Führer is a professor of scientific computations at Lund University, Sweden. He has an extensive teaching record that includes intensive programming courses in numerical analysis and engineering mathematics across various levels in many different countries and teaching environments. Claus also develops numerical software in research collaboration with industry and received Lund University's Faculty of Engineering Best Teacher Award in 2016.
Read more about Claus Führer