Strings and String Methods

Objectives ⟶

  • Recap the list data type and for loops.

  • Learn how to add, concatenate, and slice lists.

  • Discuss the string(str) data type.

  • See why strings are similar to lists.

  • Learn string utility methods.

More Collection Exercises

Collections (list and dict) are very important when working with data. Here are a few more exercises in case you need to grasp the concepts one more time.

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Slicing a List

Select Subset

In Python, you can easily slice and retrieve a subset of a list using the slice notation. The basic syntax is my_list[start:stop] where my_list is a list-like value (or a variable). start and stop specify the index where you would like to start/stop retrieving values.

Slicing

  • my_list[start:stop] retrieves all values from start to stop - 1.
  • my_list[start:] retrieves all values from start to the end of the list.
  • my_list[:stop] retrieves all values from the beginning to stop - 1.
  • my_list[:] retrieves all values from the list. This is often used to create a shallow copy of a list.
Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

A string in Python is simply a list of characters.

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading