Starting of really easy
Let's start a python interpreter like we did yesterday. Here we can play around with some basic code to learn more about python strings. Strings are words, sentences or just simple characters.
Open up a command line, type "python" and hit enter. You should see something like this:
$ python Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD)] on win32 Type "help", "copyright, "credits" or "license" for more information. >>>
To print information to the screen, we use something called "print". You can think of "print" as a function you give information and in return, "print" will show the information you give it on the screen.
A couple of examples:
>>> print("Hello") Hello >>> print('Python rocks') Python rocks
This might not be super exciting, but at least it show you how to print information on the screen.
If you look close, you can see that "Hello" has double quotes and 'Python rocks' has single quotes. You can use both, but I suggest that you stick to one of them to make your code as clean as possible.
Both "Hello" and 'Python rocks' are examples of strings. But they're not doing anything special and since we just put them right in the "print" function, we can't actually do anything with them.
To make strings a little bit more exciting, we can store them to something called a variable. You can think of a variable as a drawer, you assign it a spot (a name if our case) and you store something in them. You can store strings, numbers, objects, etc.
Below the line where we printed that Python rocks, we are going to create a variable. Type the following code and hit enter:
>>> name = 'Tim niets'
We now have a variable called "name" which contains a string with this value 'Tim Niets'. We can do a lot of things with this string. Let's first try to just print it to the screen.
>>> print(name) Tim niets
There's nothing new about this, it's just that we print a variable instead of a static string like before.
Print a greeting
We have a name stored in a variable. Let's print a greeting to this person.
>>> print("Hello " + name) Hello Tim Niets
In this little snippet, we used the "print" function to combine a static string "Hello" with the variable. Let's try to add "Hello" to the variable and print it that way.
>>> greeting = "Hello " + name >>> print(greeting) Hello Tim Niets
This gives the same result, but the way we do it is a little bit different.
Let's try a little example where we combine a string with a number.
>>> age = 32 >>> sentence = 'Age: ' + age
If you try to run this code, you will get an error. This is because you can't concatenate a string and a number. So what do you do when you want to print something like this?
Luckily, there's multiple ways of concatenating strings in python. Let's create a new "sentence" variable:
>>> sentence = 'Age:', age >>> print(sentence) Age: 32
As you can see, you'll get the correct output now. You should also notice that when you use "," to concatenate strings, you don't need a space before the quote like you did earlier.
Now you should be able to create different sentences, concatenate words with numbers and so on. Play around a little bit to become more familiar with this before you continue.
Here is a couple of more examples:
$ python Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD)] on win32 Type "help", "copyright, "credits" or "license" for more information. >>> print('Tim Niets') Tim Niets >>> name = 'Tim Niets' >>> print(name) Tim Niets >>> age = 32 >>> print('Tim Niets is', age, 'years old') Tim Niets is 32 years old >>> print('Hello ' + name) Hello Tim Niets