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

Part 3, Basic String Manipulation

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

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...

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...

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()...

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...

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...

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

Just updated your iPhone? You'll find new features for TV, Messages, News, and Shortcuts, as well as important bug fixes and security patches. Find out what's new and changed on your iPhone with the iOS 17.6 update.

14 Comments

Sorry about publishing an unfinished article, I fumbled the keyboard and somehow managed to publish it by mistake, no worries though!

hy

so i have been playing whit python and now i need to get a error if i typed somthing wrong. so example if i typ 3 i get mij own answer that i have typed in the script but if i type somthing that doesn't writing in the script i will be send back to root@appie:#

(p.s: sorry for my bad english.)

>>> word = "helloTheDefault"
>>> print word.split("")
'hello', 'The', 'Default''

Protip: To write the underscore _ here in WHT you just have to type it twice in the editor ;)

Oh, alright! First time I've commented here!
Thanks!

>>>testvar = "Null-Byte"
>>>testvar.split("-")0
'Null'

Really digging this tutorial, thank you, Defalt!

What is the role of the 0 here?

To write the [ ] here in WHT you have to type it twice in the editor.
So that line becomes

>>>testvar.split("-")[0]

And the 0 is the nth position of the string splitted, starting from 0. So, splitting "Null-Byte" with the "-" char gives 2 pieces, but since we start from 0, we can choose only 0 or 1.

>>>test = "Null-Bye-byte"
>>>test.split("-")2
'byte'

Im guessing the 2 would be the list that it is split into with 0 being 'Null' 1 being 'Bye' and 2 being 'byte'

nice tutorial though. keep up the good work

>>> test = "list a, list b, list c, list d, list e"
>>> print test.split(",")
'list a', ' list b', ' list c', ' list d', ' list e'

I think this syntax is pretty much gone with Python 3.5 right?

We use python 2.7 for this series. This should easily transfer into python 3 however.

As an exercise to get better at python3 I'm currently translating to python3 as I go. print() is the proper python3 syntax. just put the stuff to be printed (including quotes) inside the parens and you should be good to go. All other syntax works.

Share Your Thoughts

  • Hot
  • Latest