You are on page 1of 5

1/30/2021 Python for Oil and Gas Lecture 12 - Methods in Strings Part 2 - 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

Strip Method

fav_sub = input('What is your favourite subject? ')

What is your favourite subject? Drilling

print(fav_sub)

Drilling

# lstrip method

fav_sub.lstrip() /
1/30/2021 Python for Oil and Gas Lecture 12 - Methods in Strings Part 2 - Colaboratory

'Drilling'

# original variable does not change

print(fav_sub)

Drilling

# This lstrip() method not only remove the space bar. It removes any leading characters. By default it is space bar

var = '///----var....python'

var.lstrip('/-var.') # order of charachters here is not necessary

'python'

var.lstrip('v/a.r-')

'python'

# Similarly we can use rstrip()

# We can also use multiple methods in one go

print(var)

///----var....python

var.lstrip('v/a.r-').upper()

'PYTHON'
/
1/30/2021 Python for Oil and Gas Lecture 12 - Methods in Strings Part 2 - Colaboratory

Find and Replace method

additive = 'Bentonite is used to increase the density of drilling mud'

# replace Bentonite with Barite

additive.replace('Bentonite', 'Barite')

'Barite is used to increase the density of drilling mud'

print(additive)

Bentonite is used to increase the density of drilling mud

additive_2 = additive.replace('Bentonite', 'Barite')

print(additive_2)

Barite is used to increase the density of drilling mud

drill_mud = 'Drilling mud is used to lubricate the bit. This mud also helps us to achieve pressure overbalance. Drill mud also helps in lifting drill cuttings

print(drill_mud)

Drilling mud is used to lubricate the bit. This mud also helps us to achieve pressure overbalance. Drill mud also helps in lifting drill cuttings

# Replace mud with fluid

drill_mud.replace('mud', 'fluid') # this will replace all the mud with fluid

'Drilling fluid is used to lubricate the bit. This fluid also helps us to achieve pressure overbalance. Drill fluid also helps in lifting drill cuttings
'

# what if I want to replace only 1st mud

drill_mud.replace('mud', 'fluid', 1)
/
1/30/2021 Python for Oil and Gas Lecture 12 - Methods in Strings Part 2 - Colaboratory

'Drilling fluid is used to lubricate the bit. This mud also helps us to achieve pressure overbalance. Drill mud also helps in lifting drill cuttings '

drill_mud.replace('mud', 'fluid', 2)

'Drilling fluid is used to lubricate the bit. This fluid also helps us to achieve pressure overbalance. Drill mud also helps in lifting drill cuttings '

Find method

print(drill_mud)

Drilling mud is used to lubricate the bit. This mud also helps us to achieve pressure overbalance. Drill mud also helps in lifting drill cuttings

drill_mud.find('mud')

drill_mud.find('mud', 0)

drill_mud.find('mud', 10)

48

drill_mud.find('mud', 49)

105

/
1/30/2021 Python for Oil and Gas Lecture 12 - Methods in Strings Part 2 - Colaboratory

You might also like