Conditional String Concatenation

Write a Python function named conditional_concatenate that takes two string parameters. The function should return a new string where the longer string is on the outside and the shorter string is on the inside.

Example 1:

Input: conditional_concatenate('Hello', 'world') 
Output: 'worldHelloworld'

Example 2:

Input: conditional_concatenate('Python', 'fun') 
Output: 'PythonfunPython'

You can use the len function to find the lengths of the strings and determine which one is longer.

def conditional_concatenate(s1, s2):
    return s1 + s2 + s1 if len(s1) > len(s2) else s2 + s1 + s2

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!