Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm currently attempting the this problem on codeacademy. The problem is I don't know how to move letters in a string.

The attempt I made was this

oword = raw_input('enter a english word')
if len(oword) > 0 and oword.isalpha:
    step_one_new_word = (oword + oword[0]) - oword[0]

(couldn't tab so I did 4 spaces)

I was thinking that python had the strings in arrays. I'm not sure if that is the problem or if the "-" is. I need to get rid the that first letter. However I also need to move it to the end first.

I did find this answer and it seemed like it worked except I don't understand how. As you know in programming it's not like school. School they just teach you this is what you do. You do it on the test and your fine. Programming you need to know how and why it works or you'll never do anything.

Of course then I went and found this to explain substr but it still doesn't make sense.

He says this:

>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
  1. Does the side of the number determine if it returns everything but that part or just that part?

  2. How do you delete these parts?

  3. How do you select something inside the the string instead of just on one side? I am not sure but my guess it is like this [:1,2]. For example we'll use the same "Hello World!" string. This would take either "el" or "ll".

share|improve this question

closed as off topic by Bart van Ingen Schenau, Martijn Pieters, thorsten müller, World Engineer Apr 6 '13 at 19:45

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
This belong to StackOverflow –  AsTeR Apr 6 '13 at 18:54
2  
Hello and Welcome to Programmers. Please do not post Stack Overflow scoped questions to Programmers to avoid a question ban on Stack Overflow. Please read our FAQ and this post on how to remove question bans. Have a pleasant day. –  World Engineer Apr 6 '13 at 20:13
add comment

1 Answer

up vote 1 down vote accepted

First of all, there is an error in your test; you do not call .isalpha().

Strings in Python are indeed sequences, so you can slice them. To move the first letter last, select it and slice the rest with [1:]:

if oword and oword.isalpha():
    step_one_new_word = oword[1:] + oword[0]

You don't need to test for the length of the string, in a boolean context the empty string evaluates to False anyway.

You may want to read up on the syntax for slicing: The Python Slice Notation.

  1. There are 3 values, and each is optional; [start:stop:step], you need at least one colon to make it a slice. With step left to the default 1, the start and stop values default to 0 and length-of-the-sequence; the stop value is not included.

    So, [1:] means everything from the second character until the end.

    You can use negative values, these are interpreted as counting from the end.

  2. You don't delete, you create a copy of the string that doesn't include the parts you don't want. x[2:] takes everything but the first two characters.

  3. To select a substring in the middle, specify both start and stop with a colon, so x[1:3] takes the second and third character.

    'Hello World!'[1:2] would only give you e, because the stop index is not included.

share|improve this answer
    
Dude you keep editing just as I'm typing my question with the answer to my question >.< –  Griffin Apr 6 '13 at 18:04
    
So lets say the input is dog you would do use [1:] to copy the "og". Then you would do [0:0] to copy the "d"? –  Griffin Apr 6 '13 at 18:11
    
Exactly. And [:-1] to return do. Or [1:-1] to return o. –  Martijn Pieters Apr 6 '13 at 18:11
    
What if I just want the first letter? –  Griffin Apr 6 '13 at 18:12
    
Use [0] or [:1]. –  Martijn Pieters Apr 6 '13 at 18:13
show 2 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.