In this tutorial, we will take a look at exactly how to exchange 2 words in a string in Python with the aid of some instances.
If you choose a video clip tutorial over message, have a look at the adhering to video clip outlining the action in this guide.
How is exchanging words various from changing words in a String?
Note that exchanging words as well as changing words are 2 various points.
You can change a string with one more string making use of the string
replace() feature. When you’re changing a string with a brand-new string, you’re just replacing a brand-new string (that might or might not exist in the initial string) instead of the initial string Exchanging words, on the various other hand, suggests you’re changing the setting of 2 words that are currently existing in the string.
If you wish to exchange 2 personalities in a string, have a look at our detailed guide on swapping characters in a Python string.
Exactly how to exchange 2 words in a string?
Let’s take a look at an instance to see what we indicate by exchanging words in a string–
In the string, “It was drizzling pet cats as well as pet dogs”, you wish to exchange words “pet cats” as well as “pet dogs” such that the resulting string comes to be “It was drizzling pet dogs as well as pet cats”.
There’s no straight approach to exchange 2 words (substrings) inside a string in Python however we can make use of various other approaches like the complying with to attain the wanted end result–
- Converting the string right into a checklist of words.
- By chaining numerous string
replace()features with each other.
Let’s currently take a look at both the above approaches carefully.
Approach 1– Transforming the string to a checklist of words
In this approach, we develop a checklist of words from the string making use of the string
split() feature and afterwards make use of the indices of the pertinent words to exchange them in the checklist as well as lastly, obtain the resulting string back making use of the string
Let’s take a look at an instance.
# develop a string. s="It was drizzling pet cats as well as pet dogs" # develop a checklist of words from the string ls = s.split(" "). # present the resulting checklist. print( ls)
['It', 'was', 'raining', 'cats', 'and', 'dogs']
You can see that we currently have words from the string in a checklist. Currently, exchanging 2 words is as easy as exchanging 2 components in a checklist. To exchange words “pet cats” as well as “pet dogs”, we utilize their corresponding index.
# indices of components to exchange. i = 3. j = 5. # swap words at index i as well as j. ls[i], ls[j] = ls[j], ls[i]. # sign up with the checklist components with each other right into a string. outcome =" ". sign up with( ls). print( outcome)
It was drizzling pet dogs as well as cats
You can see that we obtain the resulting string with the needed words switched. Keep in mind that strings are unalterable in Python as well as for this reason can not be changed after they are developed (you can, nevertheless, reassign a variable to a brand-new string worth).
Note that if your string has spelling, you might wish to eliminate them prior to utilizing this approach. Or, attempt to divide the string such that the spelling are not with words you wish to exchange (for instance, making use of routine expressions).
Approach 2– Chaining numerous string
replace() features together
Here, we chain numerous string
replace() features with each other. The concept resembles exchanging 2 variables making use of one more momentary variable. Allow’s claim you wish to exchange
word1 as well as
word2 in a string, initially, change
word1 with a short-term string, allow’s claim
temp, currently change
word1, and afterwards, lastly, change the
temp string with
word2. The adhering to graph shows this far better.
Let’s take the exact same instance as above–
# develop a string. s="It was raning pet cats as well as pet dogs". # words to exchange. word1="pet cats". word2="pet dogs". # exchange words. outcome = s.replace( word1, "TEMPERATURE"). change( word2, word1). change(" TEMPERATURE", word2). print( outcome)
It was raning pet dogs as well as cats
Here, we are making use of the string
replace() feature 3 times. Allow’s publish out the initial string as well as the end result of each of the 3 change features to see exactly how the exchanging is happening.
print( s). print( s.replace( word1, "TEMPERATURE")). print( s.replace( word1, "TEMPERATURE"). change( word2, word1)). print( s.replace( word1, "TEMPERATURE"). change( word2, word1). change(" TEMPERATURE", word2))
It was raning pet cats as well as pet dogs. It was raning temperature as well as pet dogs. It was raning temperature as well as pet cats. It was raning pet dogs as well as cats
The over result demonstrate how we go from “It was drizzling pet cats as well as pet dogs” to “It was drizzling pet dogs as well as pet cats” utilizing this approach.
Note that the above approach will change every event of
word2 as well as vice-versa so it will not function if you wish to exchange just certain events of 2 words.
To swap 2 words, you can develop a checklist of words from the string, exchange them based upon their index as well as join them back with each other to a string. Additionally, you can chain numerous string
replace() features with each other to exchange 2 words.
In that situation, attempt to divide words from the spelling (for instance, making use of routine expressions) and afterwards develop a checklist from just words.
If there are numerous events of words to be switched in the string, we can pick which 2 certain words to exchange in the checklist approach (since it makes use of the index to swap) however the
replace() feature approach will certainly exchange every event of
word2 as well as vice-versa, that is, it will certainly not function if we wish to exchange just certain events of words.
You may additionally have an interest in–
Subscribe to our e-newsletter for even more insightful overviews as well as tutorials.
We do not spam as well as you can pull out whenever.