Last week we talked about some basic data types in Python.
If you haven’t read up on strings and lists yet, go check it out! Today, we’re going to talking about how we work with strings and lists in Python, some cool built-in functions that Python gives us to play with, and we’ll run through a few examples in Atom (my preferred text editor). So let’s get started, shall we? Yes, we shall.
There are tons of these. Here are a few that you are going to want to remember:
This is a useful method for working with strings. It converts all of the characters in a string to lowercase letters. Let’s take a look at an example.
When we run this in the terminal, it’s going to return ‘logan’. So the .lower() method goes through our code and finds all of the uppercase letters and converts them to lowercase. In this instance, it converted the ‘L’ in Logan to a lowercase ‘l’. This a useful function to have when your program relies on the user input to be in a certain case. for example, if the user types in ‘YES’, ‘YeS’ or ‘yeS’, you can convert their input to ‘yes’ so that you only have to account for one of these possibilities in your code.
Conveniently enough, Python also has a build in .upper() method, can you guess what it does? What do you think would happen if I applied this method to the string containing the value ‘Logan’ from the example below? Reply in the comments below!
Here is another great function for working with strings. Say you want to limit the length of your user input on your program for something like a user-name or password, or you have some other situation where you would like to know the length of a string. This would be a great time to use the len() function, which allows you to calculate the total ‘length’, or the number of characters in a string. Let’s go back to our example for a minute.
When we run the code len(name), the computer returns a value of 5. This is because there are 5 characters in ‘Logan”.
These are both methods that you will be using again and again in your programming journey, so you better remember them! Now, let’s discuss a few list methods while we’re at it!
You can also use len() when you are working with lists, and it can make dealing with data a lot more manageable. Say you have written a survey for a school project asking all of the students in your college to respond to a simple yes or no type question, such as do you like Dr. Pepper. more than Coke? You could store each ‘no’ response in a list, we’ll call it the_nos, and all of the ‘yes’ responses in another called the_yeses. Great. You’ve got all the data that you need, but chances are you aren’t going to want to count it all in your head. So maybe you should let Python do the hard work. Let’s see what that would look like.
So after running our code, we find the length of nos, and of yeses. We have 6 nos and 8 yeses. Sure, we could have counted that in our heads easily enough, but what about when you’re dealing with the responses of thousands of people at a time? This method is a real lifesaver!
I’m going to discuss one last method with you today, then you are free to go. It’s called .append(), and it’s great for using when you want to collect data. Say that you have written a simple todo-list app and you want to give your users the option to add on to their list at will. Great, simple enough with this method. Let’s take a look at how it works.
Now we add a new item to our todo-list, ‘Learn Python’. We run our code and we get the new list: [‘Walk the dog’, ‘Buy groceries’, ‘Learn Python’]. So .append() can be very useful for keeping track of data-items. There’s also a way to remove items from a list. See if you can Google the answer and let me know in the comments below. See you next time!