You are on page 1of 5

1/28/2021 Python for O&G Lecture 5 - String Concatenation - 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

Strings are nothing but collection of characters inside " " or ' '

String Concatenation

add the multiple strings into a single string


/
1/28/2021 Python for O&G Lecture 5 - String Concatenation - Colaboratory

first = 'Petroleum '


middle = 'From '
last = 'Scratch'

print(first + middle + last)

Petroleum From Scratch

first_2 = 'Petroleum'
middle_2 = 'From'
last_2 = 'Scratch'

print(first_2 + ' '+ middle_2 + ' '+ last_2)

Petroleum From Scratch

NOTE: Only string can be added to a string

print(first + 2)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-8510b2811acd> in <module>()
----> 1 print(first + 2)

TypeError: must be str, not int

SEARCH STACK OVERFLOW

/
1/28/2021 Python for O&G Lecture 5 - String Concatenation - Colaboratory

print(first + True)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-3e68a05b6a82> in <module>()
----> 1 print(first + True)

TypeError: must be str, not bool

SEARCH STACK OVERFLOW

print(first + '2')

Petroleum 2

print(first + str(2))

Petroleum 2

print(first + str(True))

Petroleum True

NOTE: You cannot add an integer to a string but you can multiply it to a string

print(last*5)

ScratchScratchScratchScratchScratch

ASSIGNMENT 2

print('bla bla bla bla') /


p 1/28/2021
( ) Python for O&G Lecture 5 - String Concatenation - Colaboratory

bla bla bla bla

# print the above result with the help of variable = 'bla'

ASSIGNMENT 3

Output: 'Python 3 is an easy language'

Create three varibles

first one which represents 'Python'


sec variable contains the integer 3
third variable contains the 'is an easy language'

/
1/28/2021 Python for O&G Lecture 5 - String Concatenation - Colaboratory

You might also like