- 2
Sample string: 'abc', 'xyz' and Expected result:'xyc' abz'
Swap the first two character of each string using python
5 Respostas
+ 3
def swap2(str1, str2):
return (str2[:2] + str1[2:], str1[:2] + str2[2:])
print(swap2('abc', 'xyz'))
+ 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.
+ 2
You're welcome buddy 👌
+ 1
Without using def function how can I do
+ 1
Ok thanks sir