Q:

Is it possible to flip a string in Python "around" a character?

For example let's say I have two strings:
We have the string hello-world and the string world.
I want to create a function called flip(str, arg) that basically takes the string, and "flips" the characters before and after the "arg", if any.
So hello-world passed into flip("hello-world", "-") would become world-hello and flip("world", "-") would still remain world.
An example implementation is as follows, but I am wondering if there is a library function or something for this (I only need in one location, making a function doesn't seem right). I can't seem to find what I am looking for.
def flip(s, arg):
    if arg not in s:
        return s

    index = s.find(arg)
    return s[index+1:] + arg + s[:index]

A:

You can use str.partition, then reverse the splits and join:
def flip(s, arg):
    return ''.join(s.partition(arg)[::-1])

flip('hello-world', '-')
# 'world-hello'

flip('world', '-')
# 'world'

Note that if the string has multiple occurrences of arg, the string is partitioned on the first one only.
flip("hello-my-world", '-')
# 'my-world-hello'

If you want a complete flip (reversal), you may instead consider str.split and str.join:
def flip2(s, arg):
    return arg.join(s.split(arg)[::-1])

flip2("hello-my-world", '-')
# 'world-my-hello'

