<jupyter_start><jupyter_text>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'''
print (String0 , type(String0))
print (String1, type(String1))
print (String2, type(String2))<jupyter_output><NAME> is beautiful <class 'str'>
Taj Mahal is beautiful <class 'str'>
<NAME>al
is
beautiful <class 'str'><jupyter_text>String Indexing and Slicing are similar to Lists which was explained in detail earlier.<jupyter_code>print (String0[4])
print (String0[4:])<jupyter_output>M
Mahal is beautiful<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>print (String0.find('al'))
print (String0.find('am'))<jupyter_output>7
-1<jupyter_text>The index value returned is the index of the first element in the input data.<jupyter_code>print (String0[7])<jupyter_output>a<jupyter_text>**capitalize( )** is used to capitalize the first element in the string.<jupyter_code>String3 = 'observe the first letter in this sentence. give a man a mask and he will show his true face.'
print (String3.capitalize())<jupyter_output>Observe the first letter in this sentence. give a man a mask and he will show his true face.<jupyter_text>**split( )** function is used to partition a string to a list.<jupyter_code>c = "Eat# Sleep# Conquer# Repeat"
d = c.split('#')
print (d)<jupyter_output>['Eat', ' Sleep', ' Conquer', ' Repeat']<jupyter_text>**lower( )** converts any capital letter to small letter.<jupyter_code>print (String0)
print (String0.lower())<jupyter_output>Taj Mahal is beautiful
taj mahal is beautiful<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>','New Delhi')<jupyter_output><empty_output><jupyter_text>Dictionaries (Important) 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()
print (type(d0), type(d1))<jupyter_output><class 'dict'> <class 'dict'><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 
print (d0)<jupyter_output>{'One': 1, 'OneTwo': 12}<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>print (d0['One'])<jupyter_output>1<jupyter_text>Built-in Functions **clear( )** function is used to erase the entire database that was created.<jupyter_code>database_dict = {'cat' : 100 , 'dog':50 , 'elephant':67 , 'crow':90}
database_dict.clear()
print (database_dict)<jupyter_output>{}<jupyter_text>**values( )** function returns a list with all the assigned values in the dictionary.<jupyter_code>database_dict = {'cat' : 100 , 'dog':50 , 'elephant':67 , 'crow':90}
database_dict.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>database_dict.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>database_dict.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>popped_element = database_dict.pop('cat')
print (database_dict)
print (popped_element)<jupyter_output>{'dog': 50, 'elephant': 67, 'crow': 90}
100<jupyter_text>Bonus  Concepts 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 = zip(names,numbers)
print (d2)
a1 = dict(d2)
print (a1)<jupyter_output>{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}
{'fraction_non_alphanumeric': 0.12249346120313863, 'fraction_numerical': 0.018526591107236267, 'mean_word_length': 6.764805414551607, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 75, 'https://': 0, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '412147', 'license': 'MIT', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1494, 'n_words': 499, 'path': '04_Strings_and_operations_DAC.ipynb', 'repo_name': 'pankajcode0/Python3-Tutorials', 'size': '4604'}