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

4.2. Concatenation, Indexing, and Slicing


            In this section, you’ll learn about three basic string operations:


            1. Concatenation, which joins two strings together
            2. Indexing, which gets a single character from a string
            3. Slicing, which gets several characters from a string at once


            Let’s dive in!


            String Concatenation

            You can combine, or concatenate, two strings using the + operator:

            >>> string1 = "abra"
            >>> string2 = "cadabra"
            >>> magic_string = string1 + string2
            >>> magic_string
            'abracadabra'

            In this example, the string concatenation occurs on the third line. You
            concatenate string1 and string2 using +, and then you assign the re-
            sult to the variable magic_string. Notice that the two strings are joined
            without any whitespace between them.

            You can use string concatenation to join two related strings, such as
            joining a first name and a last name into a full name:

            >>> first_name = "Arthur"
            >>> last_name = "Dent"
            >>> full_name = first_name + " " + last_name
            >>> full_name
            'Arthur Dent'

            Here, you use string concatenation twice on the same line. First, you
            concatenate first_name with " " to ensure a space appears after the
            first name in the final string. This produces the string "Arthur ", which
            you then concatenate with last_name to produce the full name "Arthur
            Dent".




                                                                          70
   66   67   68   69   70   71   72   73   74   75   76