last_two

Given a string of any length, return a new string where the last 2 chars, if present, are swapped, so “coding” yields “codign”.

This exercise was taken from codingbat.com and has been adapted for the Python language. There are many great programming exercises there, but the majority are created for Java.

Test 1

Input:

'coding'

Output:

'codign'

Test 2

Input:

'cat'

Output:

'cta'

Test 3

Input:

'ab'

Output:

'ba'

Test 4

Input:

'a'

Output:

'a'

Test 5

Input:

''

Output:

''