How does indexing a string work? or What does `word[0]` in the example mean?

Hi there, I have a question about the example before the exercise.
Why does the example use after an element in the list:
for word in words:
if word[0] == ‘@’:
usernames.append(word)

Is it because it’s a way to check the string in the list elements?

3 Likes

the examples also shows the result:

>>>print(usernames)
["@coolguy35", "@kewldawg54", "@matchamom"]

which could help use deduce what happens.

word are elements of the list, word[0] will give us the first letter. Apparently, usernames start with a @

4 Likes

words = ["@coolguy35", “#nofilter”, “@kewldawg54”, “reply”, “timestamp”, “@matchamom”, “follow”, “#updog”]
usernames =

for word in words:
if word[0] == ‘@’:
usernames.append(word)

One question: so each letter or number of an index has its own index? i mean the word variable is for all the things in the words list. by putting [number] next to it we check the character in that index position of an index?

no, look at what word is:

for word in words:
  print word

its a string, strings do have indexes

1 Like

yeah i meant a string, thats what i meant but i described it very poorly. Anyway thanks u answered to my question :slight_smile:

so how the index work in strings?
if I type word[1] will it return “c”,“k”,"m"which are the second letter in those strings?

1 Like

Correct. The second character element in a string is at index 1.

The index can be positive or negative, depending which direction we are traversing the string, left to right (positive indices or zero), or right to left (negative indices, excluding zero).

nonsense = "supercalifragilisticexpialidocious"

The positive indices, including zero will range from 0 through to len(nonsense) - 1. The negative indices will range from negative len(nonsense) to -1.

By this we can say that the last character element in the string can be found at both,

nonsense[len(nonsense) - 1]

and at,

nonsense[-1]

In general,

n = len(nonsense)

and x being an index is in range if it meets with this inequality…

{ x | -n <= x < n ; x and n are int }
2 Likes

So, if we wanted to express it in a more general way, can we say that every string (which consists of letters/symbols/numbers) is actually a list and this list’s items are each letter/symbol/number of the string with its own index?
Using for example the word(string) ‘follow’, can we think of this string like it is the list [f,o,l,l,o,w,] ?

In a sense, yes.

>>> list('string')
['s', 't', 'r', 'i', 'n', 'g']
>>> 

Both string and list objects have an __iter__ method (only in Python 3), but only lists are mutable, not strings. We can access the characters of a string as READ ONLY, unlike lists where we can both read and write to any element, or insert, append, extend or delete an element.

Their indices work exactly the same way in both cases.

In Python 2, strings are not classed as iterable, but that version is soon to be in our rear view mirror with support likely ending within the next year.

2 Likes

In this example,
for word in words:
if word[0] == ‘@’:
usernames.append(word)
Why is there a zero inside the brackets? Is it necessary, and can another number be placed inside?

you use square brackets to access strings and lists by index. So you can use a different number, but given in this case you want the first letter, zero is the right value

1 Like

In the example:

usernames = [word for word in words if word[0] == ‘@’]

word is followed by an index [0].

The solution to the problem:

can_ride_coaster = [height for height in heights if height > 161]

requires no such index.

Just wondered if someone could explain why?

Thanks in advance :slight_smile:

in the first example, you want to see if the first letter of the string equals a @, which means you need to access the string by index.

In this exercise, why does ‘usernames = [word for word in words if word[0] == ‘@’]’ have ‘word[0]’ instead of just ‘word’?

When I do the exercise, I don’t need to include ‘[0]’ after height in the list comprehension

The list comprehension iterates over a list of words, one word at a time, and appends that word to the output list if the first character is an @ symbol.

In a list such as, ['@gmail', 'yahoo!, '@msn', 'comcast'], the output list, usernames will be, ['@gmail', '@msn'].

Were we given a list of peoples’ heights,

heights = [45, 67, 53, 44, 70, 65, 46, 50, 68, 48]

and were asked to isolate only the heights greater than or equal to four feet, we would write the comprehension like so,

permitted = [height for height in heights if height >= 48]

The output will be, [67, 53, 70, 65, 50, 68, 48].

Hi, pls help me get idea of word[0]
What is 0 and how it works?
Is it item N in list? Is it =+1 automaticaly?

I get it, it is N of symbol in word… looks like words is a some type of list

Hello everyone, I was going to solve the main challenge at hand, but decided to try to solve another challenge where I append each element from heights to can_ride_coaster. With the code below, I came across an error, ‘int’ object is not subscriptable. How can I alter my code to solve this? And what does the error mean?

heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]

can_ride_coaster =

for element in heights:
if element[0] == 161:
can_ride_coaster.append(element)
print(can_ride_coaster)

element is a single member of the heights list, and of type int. Integer values cannot be treated like character sequences, which are iterable, and as such, subscriptable.

if element > 161:

Now we’re comparing an integer to another integer.

Thank you for the swift response again mtf. You are very responsive and represent Codecademy well.

That tweak solved my problem, but what is the difference between my line of code that you corrected and the example problem? Codecademy uses the boolean condition if word[0] == ‘@’, but word is also type integer. The incorporation of word is the same as my use of element. So why did their code run while mine came to the aforementioned error? Codecademy’s code is below.

I have one more question regarding the placement of the print statement in my code, but will try to figure it out on my own before asking you.

words = ["@coolguy35", “#nofilter”, “@kewldawg54”, “reply”, “timestamp”, “@matchamom”, “follow”, “#updog”]
usernames =

for word in words:
if word[0] == ‘@’:
usernames.append(word)

1 Like