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

4.3. Manipulate Strings With Methods


            Use IDLE to Discover Additional String Methods

            Strings have lots of methods associated with them, and the methods
            introduced in this section barely scratch the surface. IDLE can help
            you find new string methods. To see how, first assign a string literal
            to a variable in the interactive window:


            >>> starship = "Enterprise"

            Next, type starship followed by a period, but do not hit Enter . You
            should see the following in the interactive window:

            >>> starship.


            Now wait for a couple of seconds. IDLE displays a list of every string
            method, which you can scroll through using the arrow keys.

            A related shortcut in IDLE is the ability to use Tab to automatically
            fill in text without having to type long names. For instance, if you
            type only starship.u and hit Tab , then IDLE automatically fills in star-
            ship.upper because only one method that begins with a u belongs to
            starship.

            This even works with variable names. Try typing just the first few let-
            ters of starship and pressing Tab . If you haven’t defined any other
            names that share those first letters, then IDLE completes the name
            starship for you.


            Review Exercises

            You can  nd the solutions to these exercises and many other bonus
            resources online at realpython.com/python-basics/resources

            1. Write a program that converts the following strings to lowercase:
               "Animals", "Badger", "Honey Bee", "Honey Badger". Print each lower-
               case string on a separate line.
            2. Repeat exercise 1, but convert each string to uppercase instead of
               lowercase.



                                                                          84
   80   81   82   83   84   85   86   87   88   89   90