What are strings?

32 views
0

There is a recent tweet in which a lady said 1+2=12.

Obviously that is wrong but everyone keeps saying “it’s wrong unless we are talking about strings”

What are strings?

Edit: thank you all for explaining:) have a cookie 🍪 and a wonderful day

In: 6

If we’re talking about programming here, then simply think about using words instead of numbers. Adding two numbers equals a third number but “adding” two words (strings) will concatenate them

In this context, they’re talking about the data type used in programming used to store pieces of text. For instance, in most programming languages,

“hello, world”

would be treated as representing an object containing the text “hello, world”. Some programming languages let you use the “+” symbol to represent string concatenation (attaching) as well as addition.

“meat” + “gun” == “meatgun”

So in the twitter example, it wouldn’t be adding the numbers 1 and 2 to get 3, but rather sticking the strings “1” and “2” together to get “12”

In programming, sequences of text are often called strings (a string of letters).

Now, we can treat 1+2 as numbers, in which case: the number one added to the number two equal the number three.

or we can treat it as text: the text “1” added to the text “2” results in the text “12”.

In programming a string is a sequence of characters. It is basically text. The joke here is that 1 may be interpreted as either a string containing the single character ‘1’ or it could be the number. Some programming languages are known for often getting this wrong (JavaScript). When you add two strings together a lot of programming languages will concatenate the strings together. For example “Hello ” + “World!” would result in “Hello World!” when evaluated. And although no programming language is that bad at it you could imagine a programming language that interprets 1 + 2 as equal to “12”.

They’re speaking of strings in the computer science sense.

A string is any set of characters. Concatenating two strings — smushing together the two sets of characters — is frequently done in computer programs by using the “+” sign.

So if you have one string that is the character “A” and another that is the character “B” then when you concatenate them (smush them together) you get a single string of “AB.”

A + B = AB

Likewise if you had a string that was “E” and a string that was “LI5” when you concatenate them you get a single string of “ELI5.”

E + LI5 = ELI5

Likewise, if you have a string that consists of the character “1” and a string that consists of the character “2” then when you concatenate them you get “12.”

1 + 2 = 12

Basically it’s treating the equation as *characters* (“strings”) instead of *numbers*.