Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

String Indexing

Garrett85Garrett85 Posts: 168Member
In the code below, the only part I'M having trouble with is word[position]. I know this is something really simple but I've always had trouble understanding these kinds of statements. I know that it print a random letter (position) from (word). What I don't know is [b]why[/b] or [b]how[/b] it does that. That's the part that never seems to get explained to me. How can you just put [] around part of a statement and everything just work right? Thanks.

[code]# Random Access
# Demonstrates string indexing

import random

word = "index"
print "The word is: ", word, "
"

high = len(word)
low = -len(word)

for i in range(10):
position = random.randrange(low, high)
print "word[", position, "] ", word[position]

raw_input("

Press the enter key to exit.")[/code]
Sign In or Register to comment.