- 2

Sample string: 'abc', 'xyz' and Expected result:'xyc' abz'

Swap the first two character of each string using python

15th Feb 2020, 6:16 AM
rishi mishra
rishi mishra - avatar
5 ответов
+ 3
def swap2(str1, str2): return (str2[:2] + str1[2:], str1[:2] + str2[2:]) print(swap2('abc', 'xyz'))
15th Feb 2020, 6:34 AM
Ipang
+ 2
str1, str2 = 'abc', 'xyz' s1 = str2[:2] + str1[2:] s2 = str1[:2] + str2[2:] print(s1,s2) By making a function for it, we can reuse with other string. But without a function we must write this every time we need to.
15th Feb 2020, 7:03 AM
Ipang
+ 2
You're welcome buddy 👌
15th Feb 2020, 7:11 AM
Ipang
+ 1
Without using def function how can I do
15th Feb 2020, 6:49 AM
rishi mishra
rishi mishra - avatar
+ 1
Ok thanks sir
15th Feb 2020, 7:10 AM
rishi mishra
rishi mishra - avatar