Simple strings and variables

Hello again, today we are going to be covering the fundamentals of simple strings, and manipulating them so that they display the contents of a variable.  We will also briefly examine the input function.  Let’s take a look at the code:


So here we are printing, or displaying on the screen more accurately, the classic newbie phrase “Hello world”. This is a string, which means a list of alphanumeric characters within quotation marks.  In IDLE they are coloured green. These can be single quotes or double, but try to stick to one format consistently.

We are then being a little more sophisticated because we are introducing a variable.  In a nutshell, a variable is a section of memory that contains something.  It can hold pretty much anything, and it is the most fundamental element of programming, because as the name implies, it is changeable – as a programmer, you can assign a value to it (in this case, and input command) and then you can change it at a later date.  For this tutorial just think of it as a container.

So here the input function (functions are special commands in Python that do key things, and in IDLE they are coloured purple) does a very simple thing.  It displays the string “What is your name?” on the screen, and it waits for you to INPUT (type) a response.  That is it.

The next line then PRINTS (displays on screen) the thing that you typed in when the INPUT prompted you.  What happened there?  The results of what you typed in were stored in the name container (variable), which means that when you referred to the variable in the following print command, it looked inside the variable, found the data you typed in from the INPUT command, and then displays it back to you.  Nice, clean coding.

As we can see, and as is often the case in programming, there are multiple ways to achieve the same goal in coding.  Here, the + symbol adds the contents of the variable to the string.

You can also use a comma to indicate that you want to add the contents of the variable to the string. Note how the comma also puts a space in the text as it is printed.

Finally, this is a little more complex, but you can use another technique to add the variable contents to the string.  This is using the format method.  This is in fact the more sophisticated way in this case to add the variable contents to the string, which is why it looks more complicated.  You can do much more with it, such as left or right justifying, and you can manipulate the string much better than using + or , which is why it is considered more Pythonic, as many Python coders say. It uses little curly brackets as a sort of placeholder – so here, putting a {0} is essentially telling Python to put a little bookmark in that part of the string, and to wait for some data.  The data comes with the method .format, and the then the (name) variable.  So the code here waits until it checks what is inside the formatname variable, then puts it into the curly bracket 0 bit of the string.  Many ways to do the same thing.

We will cover many more string manipulation techniques in future posts.

Happy coding!


One response to “Simple strings and variables

  1. All in most, I give this movie a 5 away from 5 and shoot up to the peak from the Underworld series.
    As a result we have a perfect interpretation of the items was compiled by the composer,
    an incredible fusion of both technique and emotion,
    the true musical masterpiece. He was ready to pick up the pace then suddenly realized that, the goddess many artists talked just lived on the 20th Lotia street, around
    the Jeber riverside.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s