Swap Two Strings in Python Perfect Way Using Function
Without a third variable, you can swap strings in Python. With the swap function, you can achieve this. Here's the sample logic.
def swap(a, b):
![]() |
Python ideas |
Swap two strings
Multiple arguments you can use in the same function. Here, a and b are arguments for the swap function. You'll get output as swapped when you use the swap function.
def swap(a, b):
return b,a
Logic to swap strings.
i = "Hello world"
j = "This is ApplyBigAnalytics"
(i, j) = swap(i, j)
print(i)
print(j)
Logic to Swap two numbers.
i = 1
j = 2
(i, j) = swap(i,j)
print(i)
print(j)
Here is output
This is ApplyBigAnalytics
Hello world
2
1
Related Posts
Comments
Post a Comment
Thanks for your message. We will get back you.