Learning All the Things
18
Oct
Fundamentals of Python – Working with Strings! (Week 4)
Fundamentals of Python, Technology

This week is pretty strongly centered around working with Strings, whether it be using operators, indexing, or iterating over them with loops. Pretty useful stuff!

If you need access to Week 1 (Functions & Variables), Week 2 (Functions, Formatting & Assignment Statements), or Week 3 (Function and Variable Types), here they are!

Lesson 1: More str Operators

Similar to the operators from last week, strings can also be evaluated using some of the Boolean operators.

 

Operator Symbol
Equality ==
Inequality !=
Less Than <
Greater Than >
Less Than or Equal To <=
Greater Than or Equal To >=

 

Additionally, there are two other items that are covered in lesson one that are pretty freakin’ awesome and help immensely with string usage:

  1. Although not a Boolean operator, “contains” is included in this lesson. By using the “symbol” ‘in,’ you can search within a string for another string.
  2. len(“STRING“) is a function that returns the length of STRING.

Lesson 2: str Indexing and Slicing

When I started this lesson, I wasn’t honestly sure what slicing was going to refer to. Of course, the first thing they explained was that it’s essentially extraction of a substring through a variety of methods. Sweet.

First some definitions:

Index  – a position in a string. Python uses zero indexing, which means that the first position, instead of being position 1, is position 0. HOWEVER, Python also utilizes negative indexing, which does NOT have 0 as the first space, but -1. This was really confusing to me when I realized it, and I doubt I’m going to use negative indexing more than absolutely required.

Slice – A substring of a string from a start index up to but not including an end index.

Wait. What does that mean and how does it work? Allow me to demonstrate…

If s=’Happy Birthday!’

s[0:5] would display characters from position 0 to position 4, in this case ‘Happ’

s[9:] would display characters from position 9 to the end of the string, because if there is no index specified, Python assumes that the intent is the end of the string. In this case the return value would be ‘hday!’

s[9:len(s)] would display the same as s[9:] because len(s) will give return the length of the string, so ‘hday!’ for this one as well.

s[:] – since there is no designated start or end value, this would return the entire string. In this example, it would be ‘Happy Birthday!’

Additional Note from this Lesson:

You can’t CHANGE a string once it’s created, you can only grab pieces and make new ones. 🙂

Lesson 3: Methods & Functions Inside of Objects

The first point made in this lesson is that the terms values and objects are interchangeable for this course. I’m not sure if this is throughout Python as a whole, but it’s something I’m definitely going to look into. Regardless, these objects can contain functions, which we previously noted is synonymous with the term methods.  (So many words!!)

More simply stated…

A method is a function inside of an object. These can be accessed as follows:

variablename.lower -> Would return the variable’s name in all lower case letters

When you’re accessing and utilizing these methods, some may have square brackets following their designations. These square brackets, unlike round brackets, indicate parameters that are optional. This comes up especially when you’re accessing the help dir, because you’ll see various kinds of methods and bracketing structures in there. Without this information, it’s even more confusing than it would be regularly!

Here are some typical/most used string methods:

.capitalize -> Returns the string with all the first letters capitalized

.find -> locates the position of a string within another string. Searches Left to Right.

.rfind -> locates the position of a string within another string. Searches Right to Left.

.rstrip -> removes white space from a string on the right side.

.strip -> removes white space from a string on the left side.

Lesson 4: for Loop over str

First… a simple for loop, yes?

s=’Hi There!’
for char in s:
print (char)

This for loop will iterate over the entire string, and print each character in the string on a new line. Horray! Essentially, this lesson will review how to use a for loop to iterate in a string one position at a time to search for something.

Here’s another example that will count the vowels contained within in a string:

A final note here: Accumulators!

What?

Accumulators are a type of variable which… essentially accumulate values. Interestingly, these do not always have to be numeric. I’m sure we’ll get into this more in a later lesson.

Lesson 5: IDLE’s Debugger

To assist the developer in finding issues in their code during execution, they can use IDLE’s built in debugger.

Essentially, the debugger works like the visualizer from previous weeks. HOWEVER! While it lacks the pretty images, there is much more useful information presented when using the debugger, and it still works with the same stepping methodology.

Week 4 Summary

Iterating over strings is an essential portion of coding because, well, strings and how to manipulate them are pretty much unavoidable! Reviewing the for looping in this lesson was great, and I think the idea of a term for accumulators is pretty slick. Overall, a great week!

Questions/Comments?

Feel free to comment here on my blog, or find me on Twitter @DokiDara.

By Dara Monasch

2 comments on “Fundamentals of Python – Working with Strings! (Week 4)”

Leave a comment