How to Find a Character in a String: BEHIND THE SCENES!


Hi! Welcome to this section. We are going to discuss How to index a String. An incredibly powerful tool for working with strings. You will absolutely need these skills for your problem sets and future projects, so…

Let’s get started!

To index a string is to access one of it characters, but how do we know where the character we want to access is located in the string? Is there a reference to the character? Well… yes, indeed!
First, I’ve made a slide to go back a little bit and go over some common mistakes we make when we first start to work with strings.
  • Strings must always be surrounded by single or double quotes. (Always the same type of quotes. If you open the string with a single quote and close it with a double quote, or vice versa it will throw an error)
  • Any sequence of characters can be converted into a string by wrapping it in single or double quotes
  • IMPORTANT: if you must use a single quote inside the string because the sequence of characters requires it, then you must wrap the string in double quotes. As you can see on line 8, if you use a single quote inside the string, this will mark the end of the string and every character to its right will not be considered part of the string.




THEIR VERY OWN GRID!: This is BEHIND THE SCENES look at strings! :-)


You can think of strings as having their own “grid” system where every character has its own marked place that you can reference to find it.
This mark is known as an INDEX. You can find a string character by using this syntax:
Variable_containing_string[index]
IMPORTANT: I must mention that it is very important to notice that string indices start form 0 and increment by 1 to the right for every character.

At first, starting to count from 0 will be confusing to you because we usually refer to characters as “First character”, “Second character”, and so on. But remember that the actual index is 1 less than the ordinal number we give for that character.
“First character” is at index 0, and so on.
This will quickly become second nature with practice, but until that moment I recommend avoiding referring to the characters with ordinal numbers to avoid confusion.


PRACTICE TIME!

Here are some exercises for you to practice string indexing. I really recommend that you take a piece of paper and work with it, enclosing the string in a grid and assigning indices for every character.

Here are the answers! I hope you had a fun time with the exercises.
You must have done some research for the second exercise. Indexing to -1 will always return the last character in a string, no matter its length.
In computer science you will always find new features or problems and you will have to research online to find a solution. It’s great to start practicing early! :)


Hope it helps! 
If you have any questions, post them in the forums or right below this post, your classmates and Community TAs will always be there to help you.
Estefania.

Comments