Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

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

var = 'Reservoir'

# Indexing means positioning


/
1/28/2021 Python for O&G Lecture 9 - String Indexing - Colaboratory

# R = 0, -9
# e = 1
# s = 2
# e = 3
# r = 4
# v = 5
# o = 6, -3
# i = 7, -2
# r = 8 , -1

# variable_name[index pos]

var[0]

'R'

var[2]

's'

var[-1]

'r'

var[8]

/
1/28/2021 Python for O&G Lecture 9 - String Indexing - Colaboratory

'r'
var[9]

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-21-600d4ab32bb0> in <module>()
----> 1 var[9]

IndexError: string index out of range

SEARCH STACK OVERFLOW

# length function

# syntax - len(variable_name)

len(var)

var2 = 'Thiruvanantapuram'

len(var2)

17

var2[16]

/
1/28/2021 Python for O&G Lecture 9 - String Indexing - Colaboratory

'm'

/
1/28/2021 Python for O&G Lecture 9 - String Indexing - Colaboratory

You might also like