Monthly Archives: November 2017

Further string manipulation in Python

This post is picking up from the previous entry, examining simple strings and manipulating them within Python.  Previously we looked at a placeholder within a curly bracket:

1a

You can of course use multiple placeholders, and multiple brackets, when you are manipulating strings:

1b.PNG

In this case, the order in which the data is transferred to the placeholder, is determined by where they appear in the format method brackets.  So here you can see two placeholders, {0} and {1} – and the contents of the firstname variable will be transferred into the {0} placeholder; correspondingly the contents of the secondname variable will be transferred into the {1} placeholder.

For a fuller explanation see the video tutorial below:

More manipulation of strings in Python

Happy coding!

Advertisements

Talk is Cheap?

Just thought we would put this together in response to :

TalkIsCheap

Feel free to adapt and tweet via @3rdMillLearning 🙂

 

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:

variables1-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!

New to Python? Look no further…

There is an ever-increasing interest in the Python programming language.  The reasons are multiple – it is a nice, English-readable high-level language (as opposed to, for example, C#).  It is also easy to get up and running with it because it is bundled with IDLE.  You can access a Python programming environment online, through wesbites such as CodeCademy, or Trinket.  Finally, there are a large number of resources available to budding Python programmers.

Nevertheless, there seems to be a dearth of really tight, accessible videos that explain Python to the newbie – so Third Millennium Learning is launching a series of YouTube videos that do just that.  The first two can be found here, and there will be plenty of updates as the weeks and months roll by:

Introduction to Python and IDLE

IntroIDLEScreenshot

 

 

 


Introduction to using Trinket to code Python

IntroTrinketScreenshot

Happy coding!