Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes.
String0 = 'Structural Geology is fun'
String1 = "Structural Geology is fun"
String2 = '''Structural Geology
is
fun'''
print(String0 , type(String0))
print(String1, type(String1))
print(String2, type(String2))
Structural Geology is fun <class 'str'> Structural Geology is fun <class 'str'> Structural Geology is fun <class 'str'>
String Indexing and Slicing are similar to Lists which was explained in detail earlier.
print(String0[11])
print(String0[11:])
G Geology is fun
find( ) function returns the index value of the given data that is to found in the string. If it is not found it returns -1. Remember to not confuse the returned -1 for reverse indexing value.
print(String0.find('Geo'))
print(String0.find('Bio'))
11 -1
One can also input find( ) function between which index values it has to search.
print(String0.find('u', 5))
print(String0.find('u', 1, 5))
6 3
index( ) works the same way as find( ) function the only difference is find returns '-1' when the input element is not found in the string but index( ) function throws a ValueError
print(String0.index('u', 5))
print(String0.index('u', 1, 5))
6 3
capitalize( ) is used to capitalize the first element in the string.
String3 = 'observe the first letter in this sentence.'
print(String3.capitalize())
Observe the first letter in this sentence.
center( ) is used to center align the string by specifying the field width.
String0.center(70)
' Structural Geology is fun '
One can also fill the left out spaces with any other character.
String0.center(70, '-')
'----------------------Structural Geology is fun-----------------------'
zfill( ) is used for zero padding by specifying the field width.
String0.zfill(30)
'00000Structural Geology is fun'
expandtabs( ) allows you to change the spacing of the tab character. '\t' which is by default set to 8 spaces.
s = 'h\te\tl\tl\to'
print(s)
print(s.expandtabs(2))
print(s.expandtabs())
h e l l o h e l l o h e l l o
endswith( ) function is used to check if the given string ends with the particular char which is given as input.
print(String0.endswith('y'))
False
The start and stop index values can also be specified.
print(String0.endswith('n', 10))
print(String0.endswith('M', 0, 5))
True False
count( ) function counts the number of char in the given string. The start and the stop index can also be specified or left blank. (These are Implicit arguments which will be dealt in functions)
print(String0.count('u', 0))
print(String0.count('u', 5, 10))
3 1
join( ) function is used add a char in between the elements of the input string.
c = '-'.join('geology')
print(c)
g-e-o-l-o-g-y
'geology'
is the input string and char '-'
is added in between each element
join( ) function can also be used to convert a list into a string.
a = list(String0)
print(a)
b = ''.join(a)
print(b)
['S', 't', 'r', 'u', 'c', 't', 'u', 'r', 'a', 'l', ' ', 'G', 'e', 'o', 'l', 'o', 'g', 'y', ' ', 'i', 's', ' ', 'f', 'u', 'n'] Structural Geology is fun
split( ) function is used to convert a string back to a list. Think of it as the opposite of the join() function.
d = c.split('-')
print(d)
['g', 'e', 'o', 'l', 'o', 'g', 'y']
In split( ) function one can also specify the number of times you want to split the string or the number of elements the new returned list should conatin. The number of elements is always one more than the specified number this is because it is split the number of times specified.
e = c.split('-' ,3)
print(e)
print(len(e))
['g', 'e', 'o', 'l-o-g-y'] 4
lower( ) converts any capital letter to small letter.
print(String0)
print(String0.lower())
Structural Geology is fun structural geology is fun
upper( ) converts any small letter to capital letter.
String0.upper()
'STRUCTURAL GEOLOGY IS FUN'
replace( ) function replaces the element with another element.
String0.replace('fun','boring')
'Structural Geology is boring'
strip( ) function is used to delete elements from the right end and the left end which is not required.
f = ' hello '
If no char is specified then it will delete all the spaces that is present in the right and left hand side of the data.
f.strip()
'hello'
strip( ) function, when a char is specified then it deletes that char if it is present in the two ends of the specified string.
f = ' ***----hello---******* '
f.strip('*')
' ***----hello---******* '
The asterisk had to be deleted but is not. This is because there is a space in both the right and left hand side. So in strip function. The characters need to be inputted in the specific order in which they are present.
print(f.strip(' *'))
print(f.strip(' *-'))
----hello--- hello
lstrip( ) and rstrip( ) function have the same functionality as strip function but the only difference is lstrip( ) deletes only towards the left side and rstrip( ) towards the right.
print(f.lstrip(' *'))
print(f.rstrip(' *'))
----hello---******* ***----hello---
Dictionaries are more used like a database because here you can index a particular sequence with your user defined string.
To define a dictionary, equate a variable to { } or dict()
d0 = {}
d1 = dict()
print(type(d0), type(d1))
<class 'dict'> <class 'dict'>
Dictionary works somewhat like a list but with an added capability of assigning it's own index style.
d0['Azi'] = 124
d0['Inc'] = 56
print(d0)
{'Azi': 124, 'Inc': 56}
That is how a dictionary looks like. Now you are able to access '1' by the index value set at 'One'
print(d0['Azi'])
124
clear( ) function is used to erase the entire database that was created.
d0.clear()
print(d0)
{}
Dictionary can also be built using loops.
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
for i in range(len(planets)):
d0[planets[i]] = i + 1
print(d0)
{'Mercury': 1, 'Venus': 2, 'Earth': 3, 'Mars': 4, 'Jupiter': 5, 'Saturn': 6, 'Uranus': 7, 'Neptune': 8}
values( ) function returns a list with all the assigned values in the dictionary.
d0.values()
dict_values([1, 2, 3, 4, 5, 6, 7, 8])
keys( ) function returns all the index or the keys to which contains the values that it was assigned to.
d0.keys()
dict_keys(['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'])
items( ) is returns a list containing both the list but each element in the dictionary is inside a tuple. This is same as the result that was obtained when zip function was used.
d0.items()
dict_items([('Mercury', 1), ('Venus', 2), ('Earth', 3), ('Mars', 4), ('Jupiter', 5), ('Saturn', 6), ('Uranus', 7), ('Neptune', 8)])
pop( ) function is used to get the remove that particular element and this removed element can be assigned to a new variable. But remember only the value is stored and not the key. Because the is just a index value.
d1 = d0.pop('Earth')
print(d0)
print(d1)
{'Mercury': 1, 'Venus': 2, 'Mars': 4, 'Jupiter': 5, 'Saturn': 6, 'Uranus': 7, 'Neptune': 8} 3