Page 80 - Python Basics: A Practical Introduction to Python 3
P. 80

4.3. Manipulate Strings With Methods


            4.3     Manipulate Strings With Methods


            Strings come bundled with special functions called string methods
            that you can use to work with and manipulate strings. There are nu-
            merous string methods available, but we’ll focus on some of the most
            commonly used ones.

            In this section, you’ll learn how to:

             • Convert a string to uppercase or lowercase

             • Remove whitespace from a string
             • Determine if a string begins or ends with certain characters

            Let’s go!



            Converting String Case

            To convert a string to all lowercase letters, you use the string’s .lower()
            method. This is done by tacking .lower() onto the end of the string
            itself:

            >>> "Jean-Luc Picard".lower()
            'jean-luc picard'

            The dot (.) tells Python that what follows is the name of a method—
            the lower() method in this case.

               Note
               We’ll refer to string methods with a dot (.) at the beginning of
               their names. For example, .lower() is written with a leading dot
               instead of as lower().

               This makes it easier to differentiate functions that are string
               methods from built-in functions like print() and type().









                                                                          79
   75   76   77   78   79   80   81   82   83   84   85