Page 71 - tp_Modula_v2.0
P. 71

On running the above program, you will get the following output:

                     Output

                  True
                  False

                 Comparison Operators


                 Comparison operators (>, <, >=, <=, ==, !=) are used to compare two strings. It compares first
                 element from each sequence (character by character) and if they are equal then it goes on to the
                 next element until it finds elements that differ.

                 Program 10: To use membership operators.
                     Program10.py
                  File  Edit  Format  Run   Options   Window    Help
                  string1 = "apple"
                  string2 = "banana"

                  print(string1 > string2)
                  print(string1 < string2)
                  print(string1 >= string2)
                  print(string1 <= string2)
                  print(string1 == string2)
                  print(string1 != string2)


                 On running the above program, you will get the following output:

                     Output

                  False
                  True
                  False
                  True
                  False
                  True



                 String Slicing


                 Chunks of characters can be extracted from a string by using slice operator [:]. Slice of a string is
                 a extracted substring.

                 Program 11: To use membership operators.
                     Program11py
                  File  Edit  Format  Run   Options   Window    Help
                  string = "Hello, Python!"
                  print(string[:5])
                  print(string[7:])
                  print(string[-6:-1])







                                                                                      String Handling in Python   69
   66   67   68   69   70   71   72   73   74   75   76