Strings are any combination of letters, numbers and special characters. They are enclosed by single quotes(' ') or by double quotes(" "). For example,
#This is a String as it is enclosed by quotes#Extra Credit: What book did this quote come from?"All Happy families are alike and each unhappy family is unique in its own way."One might ask, "Why should the strings be under quotation marks?". The answer is simple. Without quotations, Python assumes that the thing that we typed under the print() function is a variable name(more on that later) and so it produces an error saying the variable isn't defined. The nice thing about Python is that you don't have to choose between the type of quotation mark. The not-so-nice thing is that it displays an error if you are not consistent with quotes. For example,
#You can't have something like this.."This is an error'Some examples of Strings are usernames, passwords, and sentences where no math is involved. Math is reserved for another lesson.
The fun part of a string is that you can join them. This is called concatenation.
For example,
#This is an example of concatenationprint("Hello"+"World")returns,
HelloWorldThe fun thing is that 1+2 is not 3. To prove the point, take a look,
print("1"+"2")returns,
12while,
print(1+2)returns
3See the difference? I did math on the latter but concatenation on the former. This is going to be important when we do accepting inputs from the user.
Another fun aspect is that Strings can be repeated. For example,
print("-"*3)returns,
---This is particularly useful when we do arrays and String Indexing.
1. Are these Strings?
a. "TheWhiteHouse"
b.'7 Wonders of the world'
c.'2 states"
d."She sells seashells on the seashore'