Strings: What are They?
Strings are how the computer stores textual data. When coding, we see strings as characters generally.
Strings are how the computer stores textual data. When coding, we see strings as characters generally.
Printing Strings
Strings are denoted by quotation symbols, which tell the computer that the characters inside are strings and not other bits of code. Double quotes (“”) and single quotes (‘’) are both ok, but make sure to consistently use the same one throughout all of your code!
You print strings by writing print(“_________”) OR print (‘_________’).
Strings are denoted by quotation symbols, which tell the computer that the characters inside are strings and not other bits of code. Double quotes (“”) and single quotes (‘’) are both ok, but make sure to consistently use the same one throughout all of your code!
You print strings by writing print(“_________”) OR print (‘_________’).
Notice that there is an error if you use both ‘ and “ in the same line of code. Make sure that your syntax is consistent!
Assigning Strings as a Variable
Assigning strings as variables is especially helpful when you have bigger strings. That way, you don’t need to type the string out every time you want to print it in your code.
In this example, I wanted to print the address of the Eiffel Tower. I assigned it to a variable. Then, instead of writing the address out when i wanted to print (in quotations), I wrote print(eiffelTower). Make sure that when you print variables that store strings, you DON’T use quotation marks. Otherwise, you’ll just print out the variable name.
Assigning strings as variables is especially helpful when you have bigger strings. That way, you don’t need to type the string out every time you want to print it in your code.
In this example, I wanted to print the address of the Eiffel Tower. I assigned it to a variable. Then, instead of writing the address out when i wanted to print (in quotations), I wrote print(eiffelTower). Make sure that when you print variables that store strings, you DON’T use quotation marks. Otherwise, you’ll just print out the variable name.
Instead of using quotes around the string like you normally would when printing, you can just have the variable name in the print function.
For example: print(variable_name)
For example: print(variable_name)
Concatenating String
Concatenating strings is how you write multiple strings in the same line. For example, if you have two strings that are “hello” and “world!”, you can concatenate or put them together to make “hello world!”. Make sure that when you do this, you add a “ “ in the middle so that there will be a space between “hello” and “world!”.
When you concatenate strings, add a + symbol between the strings that you want to link together. It's kind of like addition, except that you're adding strings instead of doubles or ints.
Look at the examples below to familiarize yourself with the syntax for concatenating.
Concatenating strings is how you write multiple strings in the same line. For example, if you have two strings that are “hello” and “world!”, you can concatenate or put them together to make “hello world!”. Make sure that when you do this, you add a “ “ in the middle so that there will be a space between “hello” and “world!”.
When you concatenate strings, add a + symbol between the strings that you want to link together. It's kind of like addition, except that you're adding strings instead of doubles or ints.
Look at the examples below to familiarize yourself with the syntax for concatenating.