1 of 35

Lecture 2

Printing, Strings, and Constants

Benjamin Dicken

CSc 250 - Spring 2018

2 of 35

Programming in Python

  • Using wing101 this semester
    • Go to https://wingware.com/downloads/wingide-101
    • Download and install correct version
    • Open up the program
    • Ready to start writing some code
  • If you have not downloaded it, do so now!

3 of 35

Wing 101

  • The Wing101 UI look like this
  • The main text area is where you write your code!

4 of 35

Wing 101

  • Should rearrange to look like this
  • The top area is where you write code
  • The bottom area is the Python Console. This is where the output of your python programs goes to

5 of 35

Print!

  • The first thing we’ll learn how to do is print text to the console
  • To do this, we are going to use the print function . . .

print("Text to print in here")

  • print is one of many functions that we will learn about in processing
  • This will cause the text (not including the quotes) to be printed on the console

6 of 35

Print!

  • After print runs, a line-break is added to the console
    • Like hitting “Enter” in a text editor

7 of 35

Print!

  • After print runs, a line-break is added to the console
    • Like hitting “Enter” in a text editor
  • Thus, this code:

print("Don’t know much about history")

print("Don’t know much biology")

  • Produces something different than this code:

print("Don’t know much about history Don’t know much biology")

8 of 35

Printing some lyrics

  • Song lyrics are often formatted on multiple lines
  • Write a simple python program that prints the following to the console:

If I ever took a loss, I learned a lesson

I won't ever think I'm better than the next man

I've been down before the come up, I ain't stressin'

Baby I'm too busy countin' all these blessings

(From “Blessings” by Lecrae)

ICA

9 of 35

Printing some lyrics

Solution:

print("If I ever took a loss, I learned a lesson")

print("I won't ever think I'm better than the next man ")

print("I've been down before the come up, I ain't stressin'")

print("Baby I'm too busy countin' all these blessings")

ICA

10 of 35

Print!

  • What is we need to print some very repetitive lyrics?
  • For example, the entire lyrics to Daft Punk’s “Around the World” is

Around the world, around the world

Around the world, around the world

Around the world, around the world

Around the world, around the world

(x18)

11 of 35

Print!

  • One way to print this out would be:

print("Around the world, around the world")

print("Around the world, around the world")

print("Around the world, around the world")

print("Around the world, around the world")

print("(x18)")

12 of 35

Use a variable

  • You can actually give a string a “name” and then reference the string using the name, instead of repeating the entire text

lyric = "Around the world, around the world"

print(lyric)

print(lyric)

print(lyric)

print(lyric)

print("(x18)")

  • This is actually called declaring a variable
  • We will cover this in more depth in a few lectures

13 of 35

Printing repetitive lyrics

  • Write a program that prints out these repetitive lyrics using TWO variables

Yeah, they like the way I do this

When I crank it like a chainsaw

Yeah, they like the way I do this

When I crank it like a chainsaw

(From “Chainsaw” by Family Force 5)

ICA

14 of 35

Printing repetitive lyrics

Solution:

yeah = "Yeah, they like the way I do this"

chain = "When I crank it like a chainsaw"

print(yeah)

print(chain)

print(yeah)

print(chain)

ICA

15 of 35

Use a variable

  • How about these lyrics?

Right about now The funk soul brother

Check it out now The funk soul brother

Right about now The funk soul brother

Check it out now The funk soul brother

Right about now, 'bout now 'Bout now, 'bout now

16 of 35

Use a variable

  • How about these lyrics?

Right about now The funk soul brother

Check it out now The funk soul brother

Right about now The funk soul brother

Check it out now The funk soul brother

Right about now, 'bout now 'Bout now, 'bout now

  • Where is the repetition?

17 of 35

Use a variable

  • How about these lyrics?

Right about now The funk soul brother

Check it out now The funk soul brother

Right about now The funk soul brother

Check it out now The funk soul brother

Right about now, 'bout now 'Bout now, 'bout now

  • Where is the repetition?

18 of 35

Use a variable

  • How about these lyrics?

Right about now The funk soul brother

Check it out now The funk soul brother

Right about now The funk soul brother

Check it out now The funk soul brother

Right about now, 'bout now 'Bout now, 'bout now

  • Where is the repetition?
  • There are four repetitive sections, so . . .

19 of 35

Use a variable

right = "Right about now"

check = "Check it out now"

funk = " The funk soul brother"

Bout = ", 'bout now 'Bout now, 'bout now"

20 of 35

Use a variable

right = "Right about now"

check = "Check it out now"

funk = " The funk soul brother"

Bout = ", 'bout now 'Bout now, 'bout now"

Now how do we use them?

21 of 35

Use a variable

right = "Right about now"

check = "Check it out now"

funk = " The funk soul brother"

bout = ", 'bout now 'Bout now, 'bout now"

print(right + funk)

print(check + funk)

print(right + funk)

print(check + funk)

print(right + bout)

22 of 35

String concatenation

  • When the plus-sign is placed between two strings this concatenates them together
    • Puts one after the other
  • This can be used in printing like we just saw
  • Or can be used in any case you would use a string

23 of 35

String concatenation

  • What will the another1 and another2 variables have in them?

right = "Right about now"

check = "Check it out now"

funk = " The funk soul brother"

bout = ", 'bout now 'Bout now, 'bout now"

another1 = right + right

another2 = funk + another1

ICA

24 of 35

More Strings

  • Let’s revisit the “Around the world” example:

lyric = "Around the world, around the world"

print(lyric)

print(lyric)

print(lyric)

print(lyric)

print("(x18)")

  • This is better than the original, but do you think there’s something we could do to make it EVEN BETTER?

25 of 35

Multiplication

  • In python the asterisk (*) can be used to repeat (multiply) a string some number of times

class = "CSC 250 - Essential Computing for the Sciences"

print(class * 5)

26 of 35

Multiplication

  • In python the asterisk (*) can be used to repeat (multiply) a string some number of times

class = "CSC 250 - Essential Computing for the Sciences"

print(class * 5)

  • Or . . .

name = "Your name here! " * 3

print(name * 2)

27 of 35

More Strings

  • How could we use multiplication to improve upon this?

lyric = "Around the world, around the world"

print(lyric)

print(lyric)

print(lyric)

print(lyric)

print("(x18)")

28 of 35

More Strings

  • How could we use multiplication to improve upon this?

lyric = "Around the world, around the world"

print(lyric)

print(lyric)

print(lyric)

print(lyric)

print("(x18)")

  • Will this work?

lyric = "Around the world, around the world"

print(lyric * 4)

print("(x18)")

29 of 35

Newline

  • We can put a line-break (also called a newline) into a string wherever we want with a special two-character sequence: \n
  • \n Will be replaces with a newline when the string is printed

30 of 35

Printing with newlines

  • How could we get the following to print using a single string?

AAA

BBBBBB

CCCCCCCCC

DDDDDD

EEE

ICA

31 of 35

Printing with newlines

Solution:

print("AAA\nBBBBBB\nCCCCCCCCC\nDDDDDD\nEEE")

ICA

32 of 35

More Strings

  • This is what we really need:

lyric = "Around the world, around the world\n"

print(lyric * 4 + "(x18)")

33 of 35

Which is better? Why?

print("Around the world, around the world")

print("Around the world, around the world")

print("Around the world, around the world")

print("Around the world, around the world")

print("(x18)")

lyric = "Around the world, around the world\n"

print(lyric * 4 + "(x18)")

34 of 35

Constant numbers

  • When we have a number that we want to save and use repeatedly, can store it as a CONSTANT

REPEAT = 4

lyric = "Around the world, around the world\n"

print(lyric * REPEAT + "(x18)")

  • Constants are typically in all caps
  • These should be used to store a number that will keep the same value the entire time the program runs

35 of 35

Dynamic Sizing

  • Write a program that prints out a tie-fighter
  • The width of the tie-fighter is sized according to the WIDTH constant

WIDTH = 0

/ \

<-O->� \ /

WIDTH = 5

/ \

<------O------>� \ /

WIDTH = 3

/ \

<----O---->� \ /

ICA