How do you move the first two characters of a word to the end of it?

Asked

Viewed 37 times

0

How can I move the first two characters of a word to the end of it? For example:

  • LUCAS --> CASLU
  • casa --> saca

I’ve tried using the replace transforming into list and read the first two characters [0:2],[-1:2]

  • tip: a string is a sequence object that accepts indexing

1 answer

1

As already commented, you can simply access the Index you want and rearrange them, for example with Ucas would look like this:

ss = "LUCAS"
print(ss[2:]+ss[0:2])

explaining, the ss[2:] means I’m picking from the second index to the end of the word (with nothing after the two points), ie the "CAS", then concatenating + with the ss[0:2] which are the next two letters from index 0, the "LU". Result: "CASLU"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.