3

I need to remove the first letter of a word and move it to the end, for example:

word = 'whatever'
# I want to convert it to 'hateverw'

So far I've tried this:

word[1:] # hatever

But how should I move the first letter to the end?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Marius Buivydas
  • 87
  • 1
  • 1
  • 4

3 Answers3

15

You could use:

word[1:]+word[0]
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • What is this called when you use varaiblename[1:]? I would like to research more about this to find more syntaxing info. – Crutchcorn Jan 15 '16 at 02:03
  • 1
    This is called Python [slice notation](http://stackoverflow.com/q/509211/190597), or just [slicing](https://docs.python.org/2/tutorial/introduction.html#strings). – unutbu Jan 15 '16 at 02:41
3

Here is what I did:

wrd = input("Give me a word: ")

pig = wrd[1:] + wrd[0] + "ay"

print(wrd, "in Pig Latin it is:", pig.lower())
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
Yehuda Katz
  • 177
  • 1
  • 8
1

CODE :

word=input()
print(word[1:]+word[0])

OUTPUT :

>>> oHell
>>> Hello

The code (word [1:0]) prints from the second character till the last.

If you want to write a code to print the first character then write the code as:

a="AbC"
print(a[0])

OUTPUT :

>>> A

Because the index of the string always starts from 0 and not 1.

Code Carbonate
  • 640
  • 10
  • 18