- Comments
- The
print()
function - Datatype: Strings
- String Concatenation
- String Methods
- Escape Characters
- Unit 1 Lab
# this is a comment
Comments are one of the most useful tools in programming. Comments can be used to:
# 10/07/2020
# ------------------ #
print('Welcome to Python!')
# --- say hello! --- #
print('Hello!')
# express an opinion about spam
print("I don't like spam!")
print("Hello friends!")
# print("Hello nobody!") # this line is excluded using a comment
print("Hello! Welcome to my really cool app!")
print()
is a built-in function in Python. Functions can be used to perform a variety of tasks. The parentheses are required in order for the function to work properly.
Data can be passed into the print()
function's parentheses and that data will be displayed in the terminal. If no data is privided to print()
, it displays a blank line.
print('Hello world!') # Hello world!
print() # prints a blank line
print(4 + 4) # 8
There are many different datatypes in Python. The first one we will explore are Strings. String literals represent textual data.
# Single quotes:
'allows embedded "double" quotes'
# Double quotes:
"allows embedded 'single' quotes".
#Triple quoted:
'''Three single quotes''', """Three double quotes"""
Triple quoted strings may span multiple lines. All associated whitespace will be included in the string literal.
print("""
PDX Code Guild
Programming 101
Unit 1
""")
Output
PDX Code Guild
Programming 101
Unit 1
Some examples of Strings:
# String: Hello, friends!
print("Hello, friends!")
# String: Welcome to PDX Code Guild!
print("Welcome to PDX Code Guild!")
To concatenate, or combine, two strings you can use the + operator.
Strings will be printed exactly as they're written.
print("PDX" + "Code" + "Guild") # PDXCodeGuild
If we want spaces between our strings, we have to include them ourselves.
# spaces inside another string
print("PDX" + " Code " + "Guild") # PDX Code Guild
# spaces inside their own strings
print("PDX" + " " + "Code" + " " + "Guild") # PDX Code Guild
Methods are functions that manipulate the piece of data to which they're attached. In Python, many datatypes have methods, including strings.
Find a full list of string methods here.
method | description | example | result |
---|---|---|---|
capitalize() | Converts the first letter to uppercase | "hello world".capitalize() | "Hello world" |
lower() | Converts a string to lowercase | "Hello World".lower() | "hello world" |
strip() | Removes whitespace from the beginning and end of a string | " Hello World ".strip() | "Hello World" |
title() | Converts the first character of each word to uppercase | "hello world".title() | "Hello World" |
upper() | Converts a string to uppercase | "hello world".upper() | "HELLO WORLD" |
Escape characters allow us to use special characters inside a string. They also allow us to use 'illegal' characters in a string, such as a double quote withing a string surrounded by double quotes.
An escape character is a \
followed by the character you want to insert.
code | result |
---|---|
\' |
Single Quote |
\" |
Double Quote |
\\ |
Backslash |
\n |
New Line |
\t |
Tab |
To practice everything you learned in Unit 1, please complete the following lab: