print(“Python Strings & Characters”)

print(“Python Strings & Characters”)

“Hello, World!” Do you remember our first program? We used the print() function to output a line of text. The phrase “Hello, World!” is a string, and a string is a sequence of characters. We create a string by wrapping a sequence of characters in quotation marks. Even a single character is considered a string. So it follows that a string of multiple characters is a string of strings. Which begs the question, which came first? The string or the character? The string was the characters idea for making more characters! But what exactly is a character? Do you remember our discussion on natural languages? Characters are units of information used in the written form of a natural language. Letters, numbers, punctuation marks, and even whitespace are characters. Why are they called strings? Why is a raven like a writing desk? I don’t know. But it might be useful to imagine yourself stringing characters together, one after another, like beads on a necklace.

Python Strings

Let’s begin. Open IDLE:

That was our first program. Do you remember the rush of excitement you felt when you saw those words returned on the screen? Neither do I. Regardless of what some robots will tell you, computers are totally dumb. You have to tell them what to think. So let’s try this:

Ooh, my computer thinks I’m the best. Does yours?

Why thank you, computer.

Flattery will get you everywhere.

What happened here? I was getting such great praise. Let’s look at that again.

Do you see what’s missing? We need to close our statement with quotation marks. Let’s try it again.

That’s more like it. And of course it’s your favorite language, computer.

You Can Quote Me on This

Now you can use single or double quotes to enclose a statement. For example:

And

But you can’t mix quotation marks like this:

Or like this:

Whichever quotation marks you choose, you must use the same kind. But, you ask, what if I want to print quotation marks? Easy. You simply wrap your quoted statement in the other type of marks. For example, I wrap my print statement in single quotes to be able to output double quotes:

I know. I heard you the first time, computer. You can also quote quotes like this:

You just keep telling me that, computer. And that’s it kids. You learned how to use the print() function in Python. It should keep you entertained for about 3 minutes. When the excitement wears off, perform string operations in our next lesson.

 

 

print(“Python Strings & Characters”) was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion
  • Mondie

    Great. Thanks. These lessons are a lot of fun :>)

  • Thanks! There are more in the works. Stay tuned…

  • Mondie

    One quick observation: Using Idle 2.7 and my colour scheme is not the same as your examples (mine is set to classic), is yours custom? Also, no line numbers in my version of Idle. Which is the options parameter for this to happen?
    Thanx again for sharing.

  • Our Python display is a custom plug-in for displaying code on a website. We use it because it makes reading and understanding code very easy, though it doesn’t match what you see in IDLE. You can configure your display in IDLE under the Options Menu. There you can change font settings and highlighting. Unfortunately, you can’t add line numbers to IDLE. IDLE does, however, display the line number your cursor is on in the bottom right hand corner of the window. At some point you will probably want to move beyond IDLE and there are many great IDEs that do have line numbering and are highly customizable.

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid
×