so far this is my code
def firstChars( s ):
for word in s:
print (s[0][0])
this is what it prints:
>>> s='hello','world'
>>> firstChars(s)
h
h
how do i alter this code so that it prints out 'h' and 'w'?
so far this is my code
def firstChars( s ):
for word in s:
print (s[0][0])
this is what it prints:
>>> s='hello','world'
>>> firstChars(s)
h
h
how do i alter this code so that it prints out 'h' and 'w'?
s[0]
will always be the first element in the sequence passed to the function. In order to use the specific word being examined in the for
loop just above, use word
within the loop instead.
word
represents each element of the tuple/iterable. For the tuple ("Hello", "World")
, word
will be equal to "Hello" then "World". To get the first letter of each, subscript the word
: word[0]
.
Note that this isn't the safest approach, as it will crash on an empty string (because ""
doesn't have a 0-th element). I might define a "firstLetter" function that deals with the cases of an empty string if you can't guarantee that the string won't be empty.
To elaborate on JL's answer, what you wrote runs "fine" because Strings and tuples are both iterable (like a list).
When you wrote s[0][0]
, s[0]
evaluated to "Hello", then you're left with "Hello"[0]
, which evaluates to 'H'. It output twice, once for each loop iteration because you based the loop on the tuple, which had 2 elements.
Alternative pythonic way
s='hello','world'
print('\r\n'.join(item[0] for item in s))
h
w
with the for word in s
loop, word
will first point to the first element of s
, then to the second etc. If the explanation doesn't make sense to you, just run this:
def each_word(s):
for word in s:
print s
s = ("hello", "wolrd")
each_word(s)
Now the first char of a string word
is word[0]
, and this should be enough to solve your problem.
This works. Now, one bit of warning, and it goes back to another comment about naming. In Python, it can be very hard to distinguish a string from a list or a tuple. They all iterate, they all have lengths and indices.
Be careful in how you name things, because there is no compiler to catch goofups.
def firstChars( s ):
#iterate through list/tuple passed in
for word in s:
#now, pick the first character
print (word[0])
for word in s:
print (s[0][0])
in this code fragment word
becomes each element of s
in turn -- s[0]
('hello') then s[1]
('world'). Whereas s[0][0]
directly accesses the first element of the first element of s
('h'). you are looking to access the first element of each element in turn. So:
for word in s:
print (word[0])
will do what you are trying to accomplish.
Try this:
def firstChars( s ):
for word in s:
print (word[0])
In the function firstChars
, you correctly loop over the items in s
, but note that inside the for
block, you should use word
as that is the variable that is assigned the current item in s
.
Also note that when you have multiple indices lined up as ins[0][0]
, each one "digs deeper" into the previous item. So, s[0]
refers to the first item in s
(the string 'hello'), the following [0]
refers to the first char of that item ('h').