Jump to content
  • Advertisement
Sign in to follow this  
jordi_0071

[python] array's

This topic is 4749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey i made a array code:
a = "Hello"
print "Reveres of the a string is:\n"
a[4]+[3]+[2]+[1]+[0]

the code doesnt work: cannot concatenate 'str' and 'list' objects and if i do this:
a = "Hello"
a[3]

it doesnt work either: it just doesnt show anything

Share this post


Link to post
Share on other sites
Advertisement
You can reverse the string by writing a[::-1]. It's more efficient than your version.

But your error is that you forgot to repeat the 'a'.
a[4]+a[3]+a[2]+a[1]+a[0]

[0], [1], [2] and [3] on their own are one-element list objects.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
You can reverse the string by writing a[::1]<:tt>. It's more efficient than your version.

Surely a typo: a[::-1].

Share this post


Link to post
Share on other sites
can you of someone else explain this: a[::-1]?

and if i open a new window in the shell and type the code in it and then run the module the shell only shows:

>>>
>>>

and it should be:
>>> olleH
>>>

Share this post


Link to post
Share on other sites
What we are dealing with is known as slicing, which basically amounts to selecting a subsequence (slice) of a sequence (this can be any type of ordered sequence: tuple, list, string ...). The syntax is
seq[start:end]

(where either start or end may be omitted to signify defaults of beginning and end of sequence, respectively) or, with an optional step parameter,
seq[start:end:step]

so that the slice [start, end) starts at index start, ends at index end-1 (as usual, the end of the range is exclusive), and we move in increments of step (for instance, taking every second or every third element). A few examples with strings:

>>> "hello"[1:5]
'ello'
>>> "hello"[0:4]
'hell'
>>> "hello"[0:4:1]
'hell'
>>> "hello"[0:5:2]
'hlo'
>>> "hello"[::-1]
'olleh'
>>> "hello"[4::-1]
'olleh'

Share this post


Link to post
Share on other sites
Quote:
Original post by jordi_0071
i still dont where the first : between () a[(:):-4]

Slicing works like somelist[a:b:c]

What is returned is a list [a.b) (from element index a to b-1) which is stepped through with c (so -1 would step through it in reverse, 2 would skip every other element and so on)

Quote:
Original post by jordi_0071
what about the new window and the shell question?


Share this post


Link to post
Share on other sites
if i do this:

open the IDLE >> File >> New Window >> write : a = "Hello"
a[4::-1]
print a

>> run module

then it showes just hello in the interpenter

Share this post


Link to post
Share on other sites
Original post by jordi_0071
and if i open a new window in the shell and type the code in it and then run the module the shell only shows:

If you don't print the string, the result isn't going to show up in the interpreter. Results are only printed when you use the interpreter interactively.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!