Python for Beginners, Part 6: Strings by@cleverprogrammer

Python for Beginners, Part 6: Strings

image
Clever Programmer HackerNoon profile picture

Clever Programmer

Clever Programmer is a community with over 100,000+ students who are learning to code by building real world projects.

Let's discuss the strings primitive data type and it means. This will answer the question of what's the difference between variables and strings.

In case you missed it, here are the previous parts of the series:

Transcript

00:00 Alright, in the last video we talked about variables,

00:02 you can see some basic examples.

00:04 I stored all this information in the variable bob.

00:08 And then as soon as I typed in bob,

00:09 I got back all of bob's information.

00:13 Just a quick thing I wanna say before I continue,

00:15 if any of you are having problems downloading

00:17 and installing this Python stuff that I've shown you,

00:20 if you're on Windows or a Mac or any other system,

00:24 I wanna show you a quick...

00:25 I wanna really quickly show you a website,

00:28 where you can get all of this information

00:32 or you can pretty much do everything

00:34 we've been doing right on here.

00:37 The website is called trinket.io, okay.

00:41 And here's how it works.

00:43So on the right hand side,

00:45 you can see that if I run all of this code,

00:49 it draws all of that stuff, okay?

00:52 And they have a bunch of great

00:53 examples for you guys to try out.

00:56 So you can go to Python and you can discover

00:59 a bunch of great examples that you can do.

01:02 Okay so for example, you can look at this example here,

01:07 and maybe get an idea of how to make some really

01:11 cool shapes using the turtle module in Python.

01:15 Okay, so...

01:18 Let's stop their code and let's start ours.

01:21 So if I do import turtle here,

01:23 and I do my turtle or you know,

01:27 you can call it really whatever you want.

01:29 You can call it my_john if you want,

01:31 is equal to turtle.Turtle

01:34 and if I say, my_john.forward(100)

01:41 and I run this,

01:45 invalid object has no...

01:46 Okay, I misspelled turtle here.

01:49 Run it.

01:50 And you can see that it draws a turtle going forward.

01:52 Now let me bring back their code,

01:56 and let's do it, so for example

01:59 where it says black here let's write blue.

02:03 Okay, pen color.

02:05 Background color that is.

02:07 So you see, we changed the background color.

02:09 So that's a good way to see what this stuff is doing.

02:12 You can change this color to orange maybe

02:15 and see what that does.

02:17 Okay, so the first turtle that gets drawn,

02:19 is actually being drawn by an orange color.

02:21 Okay, so...

02:23 Great place to come and try out stuff, okay.

02:26 trinket.io

02:29 Now going back to learning about strings.

02:34 So let's go over here and strings is literally

02:38 the exact thing that you type, okay.

02:40 So let's say I wanted to say, "Hello world."

02:43 If I did print hello world,

02:45 I would get back an error.

02:46 And the error would say,

02:48 "Invalid syntax, I don't know what you mean."

02:50 So let's try to type it in the Python 3 way,

02:53 where you have to put a parentheses after print

02:56 because print is something called

02:57 a function which we will cover it later.

03:00 So let's try hello like this and hit Enter.

03:04 And you can see the error I get it says,

03:07 "Hmmm, name hello is not defined."

03:10 So, it has no idea what you're talking

03:13 about when you write hello here.

03:15 It's looking for the variable hello,

03:18 we have the variable bob here,

03:20 we have the variable X,

03:21 but we never defined variable hello.

03:24 But you're like, "No that's not what I'm trying to tell it.

03:26 "I'm trying to tell it, to exactly like say hello."

03:31 Not some variable hello that might have some

03:33 numbers in it or something like that, right?

03:36 It's not like you're doing hello is equal to five

03:39 and then printing out hello and it gives you five.

03:43 Well, in this case if you want it

03:45 to say exactly what you want it to say,

03:47 you have to use something called a string.

03:49 So that just simply means, putting that thing in quotes.

03:53 So now Python goes,

03:54 "Oh, you want me to say exactly hello like this."

03:57 And it'll say hello.

03:58 Now let's say I wanted to say print hello world.

04:01 Right, if I do space here,

04:04 it's just gonna give me an error.

04:05 It's gonna be like,

04:06 a variable with a space in between a variable,

04:09 I have no idea what you mean.

04:11 So now, if you put both of these in quotes,

04:14 it'll go, oh you want the string that literally

04:17 says hello world exactly like that,

04:20 I understand, I got you, let me do it for you.

04:24 Okay, simple enough?

04:26 Cool, that's it for strings.

04:29 I'm gonna keep it really simple.

04:30 I'll see you in the next video.

Tags