Home > Software engineering >  Python | How do I swap two unknown words in an unknown string?
Python | How do I swap two unknown words in an unknown string?

Time:11-17

I cannot find how to swap two words in a string using Python, without using any external/imported functions.


What I have is a string that I get from a text document. For example the string is:

line = "Welcome to your personal dashboard, where you can find an introduction to how GitHub works, tools to help you build software, and help merging your first lines of code."

I find the longest and the shortest words, from a list, that containts all the words from the line string, without punctions.

longest = "introduction"

shortest = "to"

What I need to do is to swap tthe longest and the shortest words together, while keeping the punctions intact.

Tried using replace, but can only get it to replace 1 word with the other, but the second word remains the same.

Don't know what exactly to use or how to.

The string needs to end up from: "Welcome to your personal dashboard, where you can find an introduction to how GitHub works, tools to help you build software, and help merging your first lines of code."

When swapped: "Welcome to your personal dashboard, where you can find an to to how GitHub works, tools introduction help you build software, and help merging your first lines of code."


Tried replacing it with: newline = newline.replace(shortest, longest)

But it will only replace 1 word as mentioned before.

CodePudding user response:

You're going to need to split the text into each word and find the min/max words by their size. Afterwards, iterate through the split words and check if it's equal to either the min/max word. If it is, then you need to replace it with the proper word.

import string #to check for punctuation

line = "Welcome to your personal dashboard, where you can find an introduction to how GitHub works, tools to help you build software, and help merging your first lines of code."

words = line.split() #this includes punctuation attached to words as well

shortest = min(words, key = len) #find the length of the words that is the smallest

longest = max(words, key = len) #opposite of above

for i, word in enumerate(words): #iterate with both the index and word in the list
    if word == shortest:
        if word[-1] in string.punctuation: #check if the punctuation is at the end since we want to keep it
            words[i] = longest   word[-1] #this keeps the punctuation
        else:
            words[i] = longest
    elif word == longest:
        if word[-1] in string.punctuation:
            words[i] = shortest   word[-1]
        else:
            words[i] = shortest

line = ' '.join(words) #make a new line that has the replaced words

CodePudding user response:

If this is a homework or an assignment (as in performance does not matter) then my advice to you is to replace word 1 with {word1} and word 2 with {word2} and then do string format. The solution becomes:

line = line.replace(longest, "{word1}").replace(shorted, "{word2}")
line = line.format(word1=shortest,word2=longest)
  • Related