Swapping/changing order of a string variable using string functions

Question:
How do I make this two-word string variable print swapped using a string function, so that instead of Kim Min-Jeong, it prints Min-Jeong Kim? The other ones shown in my assignment lesson are .find(), .rfind(), and .count().
code snippet

string = "Kim Min-Jeong"
print (string.replace("Kim", "Min-Jeong"))

In the second line I use .replace() but this outputs Min-Jeong Min-Jeong and I don’t know where to go from here.

1 Like

I forgot to add that I cannot use index slices.

def swap_string(string, part_to_swap):
    return(part_to_swap + string.replace(part_to_swap, ""))

Does something like this work? I can make a more general function (swaps any 2 parts of a string ig, so Kim Min-Jeong could become Kim Jeong-Min for example)

1 Like

I think I can only combine string functions, so I can’t make any functions of my own. I just need Kim and Min-Jeong to swap places.

This is the example my teacher gave for combining string methods:

string = "Luke Skywalker"
print (string[:string.find("k")] + string[string.rfind("k")+1:])
1 Like
string = "Kim Min-Jeong"
first, *middle_last = string.split()
result = " ".join([*middle_last, first])

print(result)

This should work, I tested it and it gave the output you wanted.

A: I thought you said string indexing wasn’t allowed?

Only using string methods:

def swap_string(string, part):
    print(part.join(string.replace(part_to_swap, "")))

I don’t really see where rfind would come into play at all, as it just finds the last occurrence of the parameter (not really useful in this case)

A: Honestly, I’m confused about that too, but I think because he gave this as an example, this form of it is okay to use, just not the full on [0:5:1] kind of index.

1 Like

You can try my code, It should work.

My version:

string = "Kim Min-Jeong"
words = string.split()
print(" ".join(reversed(words)))

For any arbitrary number of ‘words’. This is, in my opinion, the most readable and intuitive.

Note: Reverses the order of all words, so if you want a different specific ordering, you’d do something similar to Sky’s answer.

2 Likes

I don’t even think OP has looked at my answer.

Yeah lol. They haven’t been online the forums in a day.

1 Like

OP was actively chatting when I posted my answer.

True, people just don’t pay attention ig. It’s also possible they didn’t need the prompt anymore (they reworked it on their own or something). Seems plausible considering they mentioned it was a school assignment…

1 Like

Eh, whoops.
It is against community standards to provide a full answer for school assignments.

When asking for help with homework, make your best effort before posting your question and do not post a full assignment. Users answering homework questions are urged not to provide full solutions or write the code for the asker! Instead, try tips, pointers, and minor debugging.

1 Like

Hey guys, please bring this topic back on topic, relevant to the question, not about the activeness of OP.

string = 'Kim Min-Jeong'
string = string.split()
newString = string[1] + " " + string[2]
print(newString)

string would turn into [“Kim”, “Min_Jeong”] and newString = “Min_Jeong Kim”

Please note what was mentioned above:

Post hints and minor debugging, not the full answer.
But with so many answers already posted… idk.

1 Like

lol
Y r you keeping up with me!?
Also, I would always help a fellow Asian XD