You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 KiB

Python Programming: Variables

A character is:

  • Anything on your keyboard , such as a letter or a number.
  • "Apple" is five characters: a, p, p, l, e.
  • Spaces count! (they're on the keyboard!)

A string is:

  • A complete list of characters.
  • "Apple"
  • "Chocolate Cupcake"
  • This entire sentence: "Hello, you are 1 of a kind!"

How Do I Create Strings in Python?

You tell Python that your variable will hold a string using quotation marks.

box_contents = "cupcakes" # This is a string
print(box_contents) # It's a normal variable - we can print it.
best_snack = "Frosted Cupcakes" # This is a string.
cupcakes_ive_eaten = 5 # No quotes - this is a number.
cupcakes_ive_eaten_as_string = "5" # Because this is in quotes, this is a string.

We Do: Declaring Strings

A "We Do" means let's practice together. Follow along!

  1. We'll declare a variable called name and assign it the value Marty
  2. We'll declare a variable called car and assign it the value Delorean
  3. We'll declare a variable called speed and assign it the string value "88"
  4. We'll print out these variables
  5. We'll add 4 to speed- what happens?

We Do: Declaring Strings


String Concatenation

+ on:

  • Numerical variables adds (5 + 5 = 10).
  • String variables concatenate ("Doc" + "Brown" = "DocBrown").
    • Pssst: Pronunciation tip: con-CAT-en-ATE
  • Numerical strings concatenate to new strings! ("5" + "4"="54"`)
first_name = "Doc"
last_name = "Brown"
full_name = first_name + last_name
print full_name
# Prints "DocBrown".

We Do: Spaces in Concatenation

It's another "We Do." Let's do this together - follow along!

To begin: sentence = name + "is driving his" + car + speed

We expect the sentence to be Marty is driving his Delorean 88mph. Is that what we got?


Strings and Printing: Review

Strings are made with quotes:

name = "Marty"
car = "Delorean"
speed = "88"

String Concatenation - we need to add the spaces!

sentence = name + " is driving his " + car + " " + speed
string_numbers = "88" + "51"
# string_numbers = 8851

To easily create spaces while printing:

print(name, "is driving his", car, speed)

Discussion: Some Common Mistakes: 1

Do you think this will run? If yes, what does it print?

my_num
print(my_num)

Discussion: Some Common Mistakes: 2

How about this? Does it run? If so, what does it print?

my_num = 5
print()

Discussion: Some Common Mistakes: 3

How about this? Does it run? If so, what does it print?

my_num = 5
my_string = "Hello"
print(my_num + my_string)

Discussion: Some Common Mistakes: 4

One last question. What does this do?

my_num1 = "10"
my_num2 = "20"
print(my_num1 + my_num2)

Q&A and Summary

We learned a lot today!

  • We created, used, and re-assigned number and string variables.
  • We used the numerical operators + - / * // %
  • We did some complex stuff with the print function!

Congrats! You've finished your first programming lesson!


Additional Resources