Page 367 - Computer Science V2.0 Class 11
P. 367

Let's Summarise


                   Ø   A string is a sequence of characters.
                   Ø   Strings can be enclosed in single quotes (' '), double quotes (" "), or triple quotes (''' ''' or """ """).
                   Ø   An escape character is a special character that is used to represent characters that are difficult to type or that
                      have a special meaning within strings. It is denoted by a backslash \ followed by another character.
                         Here are some commonly used escape characters in Python strings:
                         a.  Newline \n: Inserts a newline character.
                         b.  Tab \t: Inserts a tab character.
                         c.  Backslash \\: Inserts a single backslash.
                         d.  Single Quote \': Inserts a single quote.

                         e.  Double Quote \": Inserts a double quote.
                         f.  Null Character \0: Inserts a null character.
                   Ø   To access a character in a string, use the index of the required element within square brackets.
                   Ø   In Python, indexes for accessing string characters includes [0, len(string)-1] and [-len(string), -1].
                   Ø   (+) operator is used to concatenate two strings.
                   Ø   (*) operator produces a string, concatenated with itself a specified number of times.
                   Ø   str (): To transform a data object to a string.

                   Ø   Membership operator in returns true if a character exists in the given string.
                   Ø   Membership operator not in returns true if a character does not exist in the given string.
                   Ø   A  slice  is  marked  by  specifying  the  start,  finish,  and  step  indices  using  the  notation:
                      <start>:<finish>:<step>
                   Ø   Python also provides some built-in string manipulation methods like:
                         len(): Returns the length of a string.
                         reverse (): Returns a string having characters in the reversed sequence of the original string.
                         s.lower(): Returns lowercase version of the string.
                         s.upper(): Returns uppercase version of the string.
                         s.title(): Returns a string which has the first letter of every word of original string converted to
                         uppercase, while remaining letters are converted to lowercase.
                         s.capitalize(): Returns a string which has all the letters in the original string converted to uppercase.
                         s.isupper(): Tests whether the string comprises only uppercase characters.
                         s.islower(): Tests whether the string comprises only lowercase characters.
                         s.isalpha(): Tests whether a string comprises only alphabets.
                         s.isdigit(): Tests whether a string comprises only digits.
                         s.isalnum(): Tests whether a string contains only alphabets and digits.
                         s.isspace(): Tests whether a string comprises only whitespace characters.
                         s.startswith(subStr): Tests whether the string starts with the string subStr, passed as a parameter.

                         s.endswith(subStr): Tests whether the string ends with the suffix subStr, passed as a parameter.
                         s.find(subStr): Returns the index of the first occurrence of the string passed as an argument.
                         s.rfind(): Returns the index of the last occurrence of a substring in the given string.
                         s.index(subStr): Returns the index of the first occurrence of the substring passed as an argument.
                         s.replace(old, new): Returns a string where all occurrences of the old string are replaced by the
                         new string.

                                                                                                            Strings   353
   362   363   364   365   366   367   368   369   370   371   372