Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Strings

check

Store, manipulate, and display text with Python strings.


Strings

Now that you have been introduced to the basics of how numeric values work in Python, the next datatype to learn about are strings. Strings in Python are a collection of literal characters typically used to represent text.

Strings in Python can be created using either single (' ') quotes or double (" ") quotes. Note the examples below:

  • Defining and printing a string with single (' ') quotes:

      print('hello!')
    
  • Defining and printing a string with double (" ") quotes:

      print("hello!")
    

Like numbers, you can also create strings and assign them to variables:

  • String creation and assignment
      stringVal = "I am a string!"
    

And you can even create multiline strings if you use triple double (""" """) or single (''' ''') quotes:

  • Multiline string creation
      stringVal = """I am a string,
      That is on multiple lines,
      it can be useful for formatting
      text."""
    

Based on the above information, complete the steps below:

  1. Create a variable named first_name and set it to a single-quoted string value containing your first name.

  2. Print the value contained in the first_name variable.

    HINT*: You should see your first name output in the terminal:
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > 

    Edward

    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
      first_name = 'Edward'
      print(first_name)
    
  3. Next, create a variable named last_name and set it to a double-quoted string value containing your last name.

  4. Print the value contained in the last_name variable.

    HINT*: You should see your last name output in the terminal:
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > 

    Lorentz

    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
      last_name = "Lorentz"
      print(last_name)
    
  5. Finally, create a variable named address and set it to a multiline string value containing your postal address.

  6. Print the value contained in the address variable.

    HINT*: You should see your full address output in the terminal:
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > 

    Edward Lorentz 123 Main St. Anytown, CA, 12345

    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
      address = """
        Edward Lorentz
        123 Main St.
        Anytown, CA, 12345
      """
      print(address)
    

When you have completed the above steps, you are ready to move on to the next exercise!