How to Train Your Python: Part 3, Basic String Manipulation

Oct 10, 2015 07:42 AM
Oct 10, 2015 08:06 AM
635799812531136412.jpg

Last time in how to train you python, we covered the basics of variables and output. While we were covering variables, we talked briefly about strings. "String" is just a fancier way of saying "Word". A string is simply a set of characters encased in quotations, this lets python know that it is a word. Sometimes when we do things with strings we'll need to change them in order to do something. Python is case sensitive, for example "Null-Byte" is not the same as "null-byte". This is where manipulation comes in, we can change a string so that it is easier to use.

For Example

If we ask a user for input, whatever they enter will return as a string, so if we want to do anything with their input, we'll need to be able to evaluate it. Since python is case sensitive that makes this a bit difficult, but manipulating the string so that it is easy to evaluate will let us use it without having to know exactly what the user entered.

The Idea of Methods

635800343800023199.jpg

Methods are simply things we can use to complete certain tasks. For example (and this is what we'll be doing here today), we can use methods to manipulate strings. In the above example we had a difficult time evaluating the user input because we didn't know exactly what they were going to enter, we can use methods to convert the resulting string to all lower case or upper case. Methods are called by entering the variable name of whatever you wish to manipulate, and following it with a call to the method you'd like to use. It is structured like so...

(VARIABLE NAME).method()

To call a method on a variable, we simply call the variable and place the method at the end. You may have noticed that the method has parenthesis at the end. This is because methods can take arguments to get certain results, there will be an example of this in a minute.

Basic String Manipulation Methods

Today we'll be covering three generally useful string methods. The methods we'll be covering are...

  • .upper()
  • .lower()
  • .split()

Now the .upper() and .lower() methods are very similar, they take whatever string you give them and convert all the characters to upper or lower case. Let's use these methods and see how they return the strings...

635800346615805031.jpg

We've set the test variable equal to the string "Null-Byte". This is the variable we'll be calling our methods on. Let's try out the .upper() method first...

635800347117991790.jpg

Here we print the result of calling .upper() on the test variable and as we can see, the result was our test string in all upper case. Now that we've used .upper(), let's try using .lower()...

635800347684866923.jpg

We can see that the result of calling test.lower() on our test variable was exactly the opposite of calling .upper(). All of the characters were converted to lower case. Now that we've got a feel for .upper() and .lower(), let's cover .split(). This one is different from the other two, as it doesn't convert the characters to anything, it splits them up.

Side Note

Python has a variable type for a set of data instead of just a single value, these are known as lists, we'll be using them much more in later articles, but we don't need to dwell on them too much today, just know that a list is a set of values instead of just one.

Now, .split() takes whatever string you hand it and splits it into a list. If no arguments are given, it splits it wherever there is white (blank) space. But if we give the method a string as an argument, it will split the test string at wherever it detects the argument string. Let's try calling our split method on our test variable with no arguments first...

635800350563305742.jpg

Since the string "Null-Byte" doesn't have any white space, it wasn't split anywhere, the method simply placed the entire string within a list. Now let's try and give the split method an argument so that "Null" and "Byte" are split apart...

635800351257210746.jpg

We gave the split method the argument of a single hyphen, this means that wherever there is a hyphen in the test string, it will be split. In this case, it was split into "Null" and "Byte" because the hyphen was in between them.

That's it! We covered some basic string methods and we'll be using them on user input in the next article. These can be used to do many, many things with strings, so don't be afraid to experiment

The Exercise

Make your own test variable and try out what we did today, put your results in the comments section if you please!

Feedback

Well, that does it for this article, sorry about it being a day late, I was a strapped for time. I'll be releasing the next one tomorrow in order to get back on schedule. I'll see you for your python's next training session!

Thank you for reading!

-Defalt

Comments

No Comments Exist

Be the first, drop a comment!