
Syntax: Strings
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:
-
Create a variable named
first_nameand set it to a single-quoted string value containing your first name. -
Print the value contained in the
first_namevariable.HINT*: You should see your first name output in the terminal:
>
Edward
SOLUTION*: Click here for an example solution
first_name = 'Edward' print(first_name) -
Next, create a variable named
last_nameand set it to a double-quoted string value containing your last name. -
Print the value contained in the
last_namevariable.HINT*: You should see your last name output in the terminal:
>
Lorentz
SOLUTION*: Click here for an example solution
last_name = "Lorentz" print(last_name) -
Finally, create a variable named
addressand set it to a multiline string value containing your postal address. -
Print the value contained in the
addressvariable.HINT*: You should see your full address output in the terminal:
>
Edward Lorentz 123 Main St. Anytown, CA, 12345
SOLUTION*: Click here for an example solution
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!