You are on page 1of 6

1/28/2021 Python for O&G Lecture 3 - Escape Sequences and Raw String - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw

Let us see few more things about print function

We can use ' ' inside " " AND

We can also use " " inside ' '

print("Hello 'World'")

Hello 'World'

print('Hello "World"')

Hello "World"

But YOU CANNOT use " " inside " " AND

you cannot use ' ' inside ' '

print('Hello 'World'')
https://colab.research.google.com/drive/1PGymurQiToFexJ6xalFSNB3EhX8mmGZa#printMode=true 1/6
1/28/2021 Python for O&G Lecture 3 - Escape Sequences and Raw String - Colaboratory

File "<ipython-input-39-74fe9a9f5d54>", line 1


print('Hello 'World'')
^
SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

print("Hello "World"")

File "<ipython-input-40-5fbc5d8ba870>", line 1


print("Hello "World"")
^
SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

print('I'm Divyansh')

File "<ipython-input-41-754111731227>", line 1


print('I'm Divyansh')
^
SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

Escape Sequences

https://colab.research.google.com/drive/1PGymurQiToFexJ6xalFSNB3EhX8mmGZa#printMode=true 2/6
1/28/2021 Python for O&G Lecture 3 - Escape Sequences and Raw String - Colaboratory

# New section

# \' -> '

# \" - "

print('I\'m Divyansh')

I'm Divyansh

print("Hello \"World\"")

Hello "World"

New Line Escape Sequence (\n)

# suppose we want to print line A and line B in separate lines

print('Line 1')
print('Line 2')

Line 1
Line 2

print('Line 1 \nLine 2')


https://colab.research.google.com/drive/1PGymurQiToFexJ6xalFSNB3EhX8mmGZa#printMode=true 3/6
1/28/2021 Python for O&G Lecture 3 - Escape Sequences and Raw String - Colaboratory

Line 1
Line 2

Tab Escape Sequence (\t)

print('name:\tDivyansh')
print('age:\t24')

name: Divyansh
age: 24

\b Backspace

print('Divyana\bsh')

Divyansh

Double backslash

print(' \\ ')

print(' \\\\ ')

\\

print(' \\\\\\\\ ')

\\\\

Treating escape seuences as a normal text

# I want output: 'Line 1 \n Line 2'

print('Line 1 \\n Line 2')

Line 1 \n Line 2

# \\n

# \\ - \
# n - n

# \\n - \n

https://colab.research.google.com/drive/1PGymurQiToFexJ6xalFSNB3EhX8mmGZa#printMode=true 4/6
1/28/2021 Python for O&G Lecture 3 - Escape Sequences and Raw String - Colaboratory

# output required: \" \'

print(' \" \' ')

" '

print(' \\" \\' ')

File "<ipython-input-64-283026d81ef1>", line 1


print(' \\" \\' ')
^
SyntaxError: EOL while scanning string literal

SEARCH STACK OVERFLOW

print(' \\\" \\\' ')

\" \'

# \\ - \
# \" - "

# \\\" - \"

# Easy method for treating escape sequences as normal string

print(r' \" \' ')

\" \'

Assignment 1

# My name is 'Divyansh'
# I am 24 years old
# I love the color 'Blue'

# Generate this output using a single line code

print("My name is 'Divyansh'")


print("I am 24 years old")
print("I love the color 'Blue'")

My name is 'Divyansh'
I am 24 years old
I love the color 'Blue'

https://colab.research.google.com/drive/1PGymurQiToFexJ6xalFSNB3EhX8mmGZa#printMode=true 5/6
1/28/2021 Python for O&G Lecture 3 - Escape Sequences and Raw String - Colaboratory

https://colab.research.google.com/drive/1PGymurQiToFexJ6xalFSNB3EhX8mmGZa#printMode=true 6/6

You might also like