<jupyter_start><jupyter_text>All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures  Strings Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes.<jupyter_code>String0 = '<NAME> is beautiful'
String1 = "<NAME> is beautiful"
String2 = '''<NAME>
is
beautiful'''
String0 , String1, String2<jupyter_output><empty_output><jupyter_text>String Indexing and Slicing are similar to Lists which was explained in detail earlier.<jupyter_code>String0[4]
String0[4:]<jupyter_output><empty_output><jupyter_text>Built-in Functions **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.<jupyter_code>String0.find('al')<jupyter_output><empty_output><jupyter_text>The index value returned is the index of the first element in the input data.<jupyter_code>String0[7]<jupyter_output><empty_output><jupyter_text>One can also input **find( )** function between which index values it has to search.<jupyter_code>String0.find('j', 1, 3)<jupyter_output><empty_output><jupyter_text>**capitalize( )** is used to capitalize the first element in the string.<jupyter_code>String3 = 'observe the first letter in this sentence.'
String3.capitalize()<jupyter_output><empty_output><jupyter_text>**center( )** is used to center align the string by specifying the field width.<jupyter_code>String0.center(70)<jupyter_output><empty_output><jupyter_text>One can also fill the left out spaces with any other character.<jupyter_code>String0.center(70, '-')<jupyter_output><empty_output><jupyter_text>**zfill( )** is used for zero padding by specifying the field width.<jupyter_code>String0.zfill(30)<jupyter_output><empty_output><jupyter_text>**expandtabs( )** allows you to change the spacing of the tab character. '\t' which is by default set to 8 spaces.<jupyter_code>'h\te\tl\tl\to'.expandtabs()<jupyter_output><empty_output><jupyter_text>**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<jupyter_code>String0.index('Taj')
String0.index('Mahal', 0)
String0.index('Mahal', 10, 20)<jupyter_output><empty_output><jupyter_text>**endswith( )** function is used to check if the given string ends with the particular char which is given as input.<jupyter_code>String0.endswith('y')<jupyter_output><empty_output><jupyter_text>The start and stop index values can also be specified.<jupyter_code>String0.endswith('l',0)
String0.endswith('M',0,5)<jupyter_output><empty_output><jupyter_text>**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)<jupyter_code>String0.count('a',0)
String0.count('a',5,10)<jupyter_output><empty_output><jupyter_text>**join( )** function is used add a char in between the elements of the input string.<jupyter_code>'a'.join('*_-')<jupyter_output><empty_output><jupyter_text>'*_-' is the input string and char 'a' is added in between each element **join( )** function can also be used to convert a list into a string.<jupyter_code>a = list(String0)
''.join(a)<jupyter_output><empty_output><jupyter_text>Before converting it into a string **join( )** function can be used to insert any char in between the list elements.<jupyter_code>'/'.join(a)[18:]<jupyter_output><empty_output><jupyter_text>**split( )** function is used to convert a string back to a list. Think of it as the opposite of the **join()** function.<jupyter_code>c.split('/')<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>c.split('/',3)<jupyter_output><empty_output><jupyter_text>**lower( )** converts any capital letter to small letter.<jupyter_code>String0, String0.lower()<jupyter_output><empty_output><jupyter_text>**upper( )** converts any small letter to capital letter.<jupyter_code>String0.upper()<jupyter_output><empty_output><jupyter_text>**replace( )** function replaces the element with another element.<jupyter_code>String0.replace('<NAME>','Bengaluru')<jupyter_output><empty_output><jupyter_text>**strip( )** function is used to delete elements from the right end and the left end which is not required.<jupyter_code>f = '    hello      '<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>f.strip()<jupyter_output><empty_output><jupyter_text>**strip( )** function, when a char is specified then it deletes that char if it is present in the two ends of the specified string.<jupyter_code>f = '   ***----hello---*******     '
f.strip('*')<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>f.strip(' *'), f.strip(' *-')<jupyter_output><empty_output><jupyter_text>**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.<jupyter_code>f.lstrip(' *')
f.rstrip(' *')<jupyter_output><empty_output><jupyter_text>Dictionaries 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()<jupyter_code>d0 = {}
d1 = dict()
d0, d1<jupyter_output><empty_output><jupyter_text>Dictionary works somewhat like a list but with an added capability of assigning it's own index style.<jupyter_code>d0['One'] = 1
d0['OneTwo'] = 12 
d0<jupyter_output><empty_output><jupyter_text>That is how a dictionary looks like. Now you are able to access '1' by the index value set at 'One'<jupyter_code>d0['One']<jupyter_output><empty_output><jupyter_text>Two lists which are related can be merged to form a dictionary.<jupyter_code>names = ['One', 'Two', 'Three', 'Four', 'Five']
numbers = [1, 2, 3, 4, 5]<jupyter_output><empty_output><jupyter_text>**zip( )** function is used to combine two lists<jupyter_code>d2 = list(zip(names, numbers))
d2<jupyter_output><empty_output><jupyter_text>The two lists are combined to form a single list and each elements are clubbed with their respective elements from the other list inside a tuple. Tuples because that is what is assigned and the value should not change.Further, To convert the above into a dictionary. **dict( )** function is used.<jupyter_code>dict(d2)<jupyter_output><empty_output><jupyter_text>Built-in Functions **clear( )** function is used to erase the entire database that was created.<jupyter_code>d1.clear()
d1<jupyter_output><empty_output><jupyter_text>Dictionary can also be built using loops.<jupyter_code>for i in range(len(names)):
    d1[names[i]] = numbers[i]
d1<jupyter_output><empty_output><jupyter_text>**values( )** function returns a list with all the assigned values in the dictionary.<jupyter_code>d1.values()<jupyter_output><empty_output><jupyter_text>**keys( )** function returns all the index or the keys to which contains the values that it was assigned to.<jupyter_code>d1.keys()<jupyter_output><empty_output><jupyter_text>**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.<jupyter_code>d1.items()<jupyter_output><empty_output><jupyter_text>**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.<jupyter_code>d2 = d1.pop('Four')
d1, d2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11079102148601608, 'fraction_numerical': 0.010803024846957148, 'mean_word_length': 6.794200187090739, 'pattern_counts': {'":': 0, '<': 161, '<?xml version=': 0, '>': 161, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '39', 'dir': 'jupyter-structured-clean-dedup', 'id': '309136', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2678, 'n_tokens_neox': 2613, 'n_words': 873, 'path': '04.ipynb', 'repo_name': 'georgi/Python-Lectures', 'size': '8343'}