Skip to content

Chapter 2

Jarlin Almanzar edited this page Oct 22, 2019 · 16 revisions

Naming and Using Variables

Contain

  • Only letters
  • Only numbers
  • Only underscores
  • Can start with an underscore or letters, not numbers

Can't Contain

  • spaces
  • keywords
  • function names

Variables and Simple Data Types

Variables are labels for values variable_name = variable_value

Data Types

Strings

A series of characters

Anything inside "this" or 'this' is considered a string

" " allow for for quotes and apostrophes , while '' does not

f" " Allows us to insert a variable into a string, called f-strings

f being for format, b/c Python formats the string by replacing the name of any variable in brace

Variable String Example

#variable
message = "Hello World"
print(message)

Changing Case

# title() => changes each word to title case, where each word begins with a capital letter
message = "hello world"
print(message.title())
# upper() => changes each character to be capitalized 
# lower() => changes each character to be lower case
message = "hello world"
print(message.upper())
print(message.lower())

Variables in String

first_name = "Ava"
last_name = "Montecristo"
full_name = f"{first_name} {last_name}"
print(f"Hello, {full_name.title()}!")
Clone this wiki locally