w3resource logo


Python Exercises

Python: Program to get a single string from two given strings, separated by a space and swap the first two characters of each string


Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string.

Sample String : 'abc', 'xyz'
Expected Result : 'xyc abz'

Sample Solution :

Python Code :

def chars_mix_up(a, b):
  new_a = b[:2] + a[2:]
  new_b = a[:2] + b[2:]

  return new_a + ' ' + new_b
print(chars_mix_up('abc', 'xyz'))

Console :

Copy and paste the above code and press "Enter key" to execute :

Post your code through Disqus