So you have set up your terminal and are executing some basic code as seen in part one.
Assigning variables in Python
Let’s get started by looking at setting up variables that we can use in our python code. Variables in python can be any name that we can think of that we can assign some data to; with the exception that they cannot be named the same as key functions (like print for example) or contain special characters (£, $,%, etc). If you do make a mistake when setting up a variable name you will see an error message like the one below notifying you of this fact.
Variable can contain strings (text), numbers, or boolean values (true of false). You can use variables to execute code like in the example below.
example_string = “This is an example!”
In the above case, example_string is the variable that we are assigning the string “This is an example!”. Instead of typing this directly inside the print function as we see used in part one, we are able to call the variable within the print function itself.
Printing to a new line with the escape character
If you wish to print onto a new line, instead of using another separate print function, you can use \n within a string which will place the text after this escape character onto the next line as you can see in the example below.
Finding the length of a given string in Python
Often in Python guides you will see examples of how to find the length of a string. So we will not overlook this in our own guide here! You may not be able to think of many examples of how to use this function, though this can be very handy. You can use the length function to make sure that a user has given enough information in a form; meeting minimum username or password lengths; or even within a computer game program.
To use this function we use the len() command which is similar to print() where we again can place numbers, text, or variables within the parenthesis.
random_string_length = len(“This is an example string”)
You can see from the above image that we can back a result of 25. This is the number of characters (letters and numbers) that are included in the string. It is also important to note that spaces are also counted as a character, along with other special characters.
How to concatenate strings
Concatenation is simply the way to add strings and numbers together.
variable1 = “The age of my dog is “
variable2 = 2
print(variable1 + str(variable2))
From the above example you can see that the first time we attempted to concatenate the variables that we receive an error message. This is because the concatenate function can only join together variables of the same type. In this case we are attempting to join a string and an integer which results in the error.
The second print statement shows the solution to this by using str(). When variable2 is enclosed in the string function this changes it from an integer to a string. Since both variables enclosed in the print function are now both strings, the concatenate function works as expected.
Lets continue to part three to look at more Python examples.