Sets and Strings.



👈Previous Topic


Hello Folks....!!

Today we are going to see the data structure "Sets" and an important concept which you need for placements-"Strings".

Sets :

  • Sets are also an iterable container like lists and tuples.
  • These are mutable.
  • The only difference between Sets, lists and tuples is that, in Sets the elements cannot be repeated, ie., duplicates are not allowed.
  • These are enclosed within the curly braces - '{ }'.
  • To make a set immutable, we declare them using frozenset().
  • Examples :
python set and frozen set

Methods associated with sets :

1. add(item) : To add an item to the set. It adds the element only if it is not already present in the set. See the example below. 

python-tuple-additem()

2. union(set) : To get all the elements in both the sets together in one. The syntax is new_set= set1.union(set2). You can also use the operator '|' and write as new_set= set1|set2.

3. intersect(set) : To get the common items from the two sets. The syntax is new_set=set1.intersect(set2)

4. difference(set) : To get the items from the (calling set) set1 which are not present in the second set. The syntax is new_set=set1 . difference (set2). You can also write new_set=set1-set2.

5. clear() : Empties the set

The main disadvantage of sets is that, it does not maintain its elements in any specific order. That is why we don't use it much.

Strings

 As in C or C++, Strings in python are also array of characters. They are written either within single quotes or double quotes. There are several methods associated with Strings.

Accessing strings 

Consider the string "Hello" :

Python-strings

Like lists, one can access the strings using their index. They also have positive and negative indexing. 

Slicing technique : like the lists, the Strings can also be sliced using their index. Recall slicing of lists

String and Built-in Methods :

1. split()

  • If a sentence is given, it splits the sentence on the given delimiter. 
  • The default delimiter is space. 
  • The syntax is string_name.split(delimiter).
  • The out come will be the words are characters after the specified delimiter in the list form.
  • For example :
s1="Hello World !!!"
print(s1.split())
output: ["Hello","World",''!!!'']
  • Now on the resultant list, one can apply all methods associated with the lists.
  • In the place of delimiters one can use any symbol that appears in the sentence. Not only symbols, if you want to split at every 'a' or '1' in the sentence, you can also mention them.

2. find() and rfind() :

  • These methods returns the lowest(first occurrence) starting index of the substring we are searching for.
  • The find() searches from left to right, rfind() searches from right to left. It returns the highest starting index.
  • The return an integer which is the index where the substring starts.
  • These methods count the spaces between the words also.
  • You can also pass the starting and ending index as a parameter, so that the method will search in that range only.
Python-strings
  • In the above example, in third line of code, the start and end parameters are passed. It specifies that search must start from the index 0 and end at 20. If the substring is found it returns the index, otherwise it returns '-1'. 
  • When you use rfind(), it returns the highest index of the given substring. 
  •  For example :
s1="There was a lazy fox in the forest.The fox lived near the river."
print(s1.find('fox'),s1.rfind('fox'))
output : 17 39
 
Here, the rfind()'s output is 39. It is because (here the second occurrence) the index of 'fox', which is the highest is 39.

3. replace() :

  • To replace a character or word in the given string.
  • The syntax is string_name.replace(w1,w2,no_of_times).
  • In the above syntax 'w1' is replaced with 'w2'. the last argument is optional and by default 1. It denotes, if a word occurs more than once, how many occurrences have to be replaced.

4. capitalize()

  • Converts the first letter to capital.
  • The syntax is string_name.capitalize().

5. upper() and lower() :

  • Converts the whole string to uppercase and lowercase respectively.
  • The syntax is string_name.upper() string_name.lower().

6. string_name.isupper() : Returns true if the string is in uppercase.

7. string_name.islower(): Returns true if the string is in lowercase.

8. len(string_name) : Returns the length of the string.

A Technique for reversing the string using slicing technique :

string_name[string_length::-1] 
or 
string_name[::-1]

In the above syntax, [::-1] - means start from the end(string's length) and go till '0', in reverse-'-1'


Thatsall folks.... We have successfully completed all the data structures in python. Next week onward, we will see the looping concepts and programs. Tomorrow I will be posting some exercises on topics posted till today. Have a glance and work on it.

Next Topic👉

Comments