swap a string in python
Code snippet on how to swap a string in python
s1 = "Hello"
s2 = "World"
s1, s2 = s2, s1
print(s1)
print(s2)
This code swaps the strings stored in the variables s1
and s2
. It starts off by defining the strings "Hello"
and "World"
and storing them in the variables s1
and s2
, respectively.
Next, on line 3, it uses a multiple assignment to set the variables s1
and s2
to their current swapped values, that is s2
and s1
. Then, it prints out the contents of these two variables in order to verify that the strings have indeed been swapped.