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

●  s.capitalize(): The method capitalize() returns a string which has the first letter of first word in the
                   original string converted to uppercase, while the remaining letters are converted to lowercase.
                  >>> message = "Hello \'How are You?\'I am Good, How are You?"
                  >>> message.capitalize()
                      "Hello 'how are you?'i am good, how are you?"


                        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 the first letter of first word in the original string converted to
                        uppercase, while the remaining letters are converted to lowercase.


                 ●  s.strip(),  s.lstrip(),  and  s.rstrip():  The  method  strip()  returns  a  string  formed  from  the
                   original string by removing the whitespace characters (space, tab, linefeed, return, form feed, and vertical tab)
                   appearing at the beginning and end of the string. For example,

                  >>> '        Hello How are you!      '.strip()
                      'Hello How are you!'
                 Note that the method removes only the leading and trailing spaces, the white space appearing elsewhere is retained.
                 Let us examine another example,
                  >>> msg = '''        Hello How are you!
                  ... I am Good,    Sir,
                  ... How are you '''
                  >>> msg1 = msg.strip()
                  >>> msg1
                      'Hello How are you!\nI am Good,   Sir,\nHow are you'
                  >>> print(msg1)
                      Hello How are you!
                      I am Good,   Sir,
                      How are you
                 To remove the whitespace only from the beginning or end of the string, we use the Python string methods lstrip()
                 and rstrip(), as illustrated below:
                  >>> '       Hello How are you!      '.lstrip()
                      'Hello How are you!      '
                  >>> '       Hello How are you!      '.rstrip()
                      '       Hello How are you!'

                        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 s starts with the string subStr, passed as a parameter.
                        s.endswith(subStr): Tests whether the string s ends with the suffix subStr, passed as a parameter.


                 ●  s.isupper():  The  method  isupper()  tests  whether  the  string  comprises  only  uppercase  characters.  For
                   example,
                  >>> msg1 = 'SURAIYAJABIN'
                  >>> msg1.isupper()
                      True


                                                                                                            Strings   343
   352   353   354   355   356   357   358   359   360   361   362