The Value Of Zoom Networking With Other Freelance Writers

Physically removed from coworkers, the water cooler, and sometimes other human company, it’s easy to become socially isolated while making a living writing from home over the internet. Compound that…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Problems

Learn to Program — by Chris Pine (19 / 105)

👈 12 vs. ’12’ | TOC | Chapter 4 Variables and Assignment 👉

At this point you may have tried some things that didn’t work. If not, here are a few:

Hmmm…an error message. The problem is that you can’t really add a number to a string or multiply a string by another string. It doesn’t make any more sense than this does:

Here’s something else to be aware of: you can write ‘pig’*5 in a program, since it just means five sets of the string ‘pig’ all added together. However, you can’t write 5*’pig’, since that means ‘pig’ sets of the number 5, which is…poetic, at best.

Finally, what if we want a program to print out You’re swell!? We can try this:

Well, that won’t work; I can tell that just from the syntax coloring. I won’t even try to run it. The problem is that your computer can’t tell the difference between an apostrophe and a single quote (to end the string). I think the confusion is reasonable here, though. They are the same character, after all. We need a way to tell the computer “I want an apostrophe here, inside this string.” How do we let the computer know we want to stay in the string? We have to escape the apostrophe, like this:

The backslash is the escape character. In other words, if you have a backslash and another character, they are sometimes translated into a new character. The only things the backslash escapes, though, are the apostrophe and the backslash itself. (If you think about it, escape characters must always escape themselves, too, to allow for the construction of any string. Why is that?)

Let’s see a few examples of escaping in strings:

Since the backslash does not escape a d but does escape itself, those last two strings are identical. Obviously they don’t look the same in the code, but when your program is actually running, those are just two ways of describing identical strings.

You good so far? Good. Let’s start doing something slightly more clever….

Copyright © 2014, The Pragmatic Bookshelf.

👈 12 vs. ’12’ | TOC | Chapter 4 Variables and Assignment 👉

Add a comment

Related posts:

Aufgeben oder weitermachen?

Als ich mit dem MindPunk Blog gestartet bin, war ich hoch motiviert. Ich dachte, wenn ich es schaffe jeden Tag einen Blog zu schreiben, wird einfach alles wunderbar werden. Doch dann kam die…

Facebook joins Apple to limit screentime

Facebook and Instagram are to join Apple’s lead in releasing a tool that limits how much time people spend on their apps. Meanwhile, the Royal Public Health Society is behind a new UK campaign to…