Python: How to Swap Variables with Tuple

Swap two strings no need to define third variable in Python explained. To achieve this, you need a tuple, explained with an example of how to do it.

Python Swap Strings without Third Variable
Python ideas

How to Swap Two Strings in Python

You can swap two strings in Python using a tuple. You can have multiple values in a tuple. In this example, I have given a and b. When you use the swap function, it swaps the values.


def swap(a, b): 
return b,a 


Example to Swap Two Strings.

i = "Hello world"
j = "This is ApplyBigAnalytics" 

i, j = swap(i, j) 

print(i) 
print(j) 


Example to Swap Two Numbers.

i = 1 
j = 2 

i, j = swap(i,j) 

print(i) 
print(j)


The Result is:

This is ApplyBigAnalytics 
Hello world 

Comments

Popular Posts

7 AWS Interview Questions asked in Infosys, TCS

Hyperledger Fabric: 20 Real Interview Questions

How to Fix Python Syntax Errors Quickly

Python 'getsizeof' Command the Real Purpose

How to Check Log File in Kafka

5 HBase Vs. RDBMS Top Functional Differences

Python Dictionary Vs List With Examples

Blue Prism complete tutorials download now

Linux Relative Vs. Absolute Path Top Differences

How to Use the ps Command in Linux